Error with Oauth in Facebook JS SDK - javascript

I am encountering a weird issue when trying to do an advanced login flow using the Facebook API with long-lived tokens stored on my server (https://developers.facebook.com/docs/facebook-login/access-tokens#long-via-code)
The issue is this: Upon shipping a generated code back to my client (a JS web app in this case) I should be calling the graph API to generate a long-lived token. My call looks something like this:
var requestUri = "/oauth/access_token?code=" + code + "&client_id=" + facebookAppId + "&redirect_uri=" + redirectUrl;
FB.api(requestUri, function(response) {
console.log(response);
});
However, upon callback of this FB.api request, I get a response that looks like the following:
{
"error": {
"message": "unknown error",
"type": "http"
}
}
There is also a JS error in the console:
Uncaught SyntaxError: Unexpected token :
What is really strange about all of this is that if I look at the raw response of the call to /oauth/access_token I see the expected response with a 200 success code
{
"access_token": "...",
"machine_id": "...",
"expires_in": 5184000
}
I can determine from this that the JSON response isn't be read correctly by my browser (Chrome in this case). In fact if I paste the raw response above into Chrome Dev JS Console, it gives me the same JS error. However, if I were to assign it to a variable in the console, there are no problems. It basically seems as though something is going wrong in Facebook's JS SDK and the response object never is properly returned to the callback function. Has anyone encountered anything like this before? I can't seem to find an answer for this.

I did find a workaround for this for the time being. I'm pretty sure the problem stems from somewhere in the Facebook JS SDK. To get around this, I made a raw call using AJAX to the graph API, in this case using AngularJS
$http.get("https://graph.facebook.com/oauth/access_token....)
This way, I can get the response I need without having to deal with an error that I can't get to.
I can also get away with not using FB.api because I am requesting an access token and don't need to worry about the JS SDK managing it for me.
If anyone has any other solutions to the initial problem, I would be delighted to hear them. I still would like to use Facebook SDK wherever possible.

Related

sharePoint online count unread message from office365

As part of a project whose aim is to notably improve the visual side of a SharePoint Online site, I'm a bit stuck. On the home page in the left banner, users want to see the number of unread messages they have in Office365.
I created an area in the master page to put the result in. I thought the Rest API used to do this :
$.ajax ({
type: "GET",
url: " https://outlook.office365.com/ews/odata/Me/Folders/Inbox",
dataType : "json",
success : function (resp) {
// count unread messages
},
error : function (e) {
alert (' Error121212 :' + JSON.stringify (e));
}
})
Unfortunately I get an error like cross domain. I tried with JSONP but it does not work either (uncaught syntax error unexpected token).
Can you please tell me if this is a good practice? I feel that it anyways I must find a technic for authentication. (In the case of JSONP I have a popup that asks me authentication and then problem occurs on callback apparently)...
I want to avoid developing a type requiring a typical deployment Wsp...
Thank you in advance for your help.
Your URL for the ajax request seems incorrect. The URL for getting the inbox messages via the API is: https://outlook.office365.com/api/v1.0/me/folders/inbox/messages
Once you get the response, you can count the number of objects with the IsRead property set to false using a simple for loop and display that count.
The issue here is related to CORS and how browsers refuse to handle cross-domain requests. To get around this, typically you would either
Change the response header on the remote server - not an option here
Use some sort of proxy to handle the requests - here's where SharePoint apps come in.
I know you stipulated that you want to avoid using a WSP style deployment but there simply isn't a way around it, you have to use the SharePoint App Model
This article goes a long way to answer your question, but for completion the basic steps are as follows
Create a SharePoint hosted app in Visual Studio
In the App Manifest, you need to define the trust relationship with the remote host (in this case the host of outlook.office365.com) using the AppManifest section
Use SP.RequestExecutor.executor to make the request on your behalf

Google APIs Client Library for JavaScript - 404 on method call

Since this morning all my applications using gapi are down.
I'm using:
https://apis.google.com/js/client.js
to communicate with the Endpoints of my Google Appengine applications, for example:
gapi.client.load('public', 'v2', function(){
gapi.client.public.organizations().execute(function(response){
console.log(response);
});
}, 'https://XXX.appspot.com/_ah/api');
As of today all calls are responded with the following error message:
[{"error":{"code":404,"message":"Not Found","data":[{"domain":"global","reason":"notFound","message":"Not Found"}]},"id":"gapiRpc"}]
My applications are not logging any errors.
I can reach the Endpoint API explorer (/_ah/api/explorer) without errors.
I can make HTTP-request calls without errors, e.g
https://XXX.appspot.com/_ah/api/public/v2/organizations
The "gapi"-object is loaded without errors. My "public" endpoint is also loaded and I can list all methods using the javascript console.
I have reported this error to Google.
Anybody else having this issue? Does anybody have any quick solutions or workarounds? Have I perhaps missed some Google updates or API-changes?
Thanks
It seems to be a general issue with the JS Client library at the moment, not limited to Endpoints APIs, but affecting all Google APIs.
https://code.google.com/p/google-api-javascript-client/issues/detail?id=136
Only real "work around" is not to depend on the JS Client Library (which had stability issues in the past as well) and construct the HTTP Requests yourself, which I know isn't a quick solution.
You can also try using the gapi.client.request method for direct REST requests which seems to be working for one of my endpoints APIs. (again, not a quick solution, but probably better/easier since you still have the authentication working via the client library).
gapi.client.request({
"path": "/public/v2/organizations",
"root": "https://XXX.appspot.com/_ah/api"
}).execute(function (response) {
console.log(response);
});
Edit: Update from the linked issue
They will be rolling back the broken update which will take several hours to complete (no exact ETA yet).
As a "quick" fix you can explicitely add the apiVersion to each request (careful: the B might change after the rollback, but it works now):
var request = gapi.client.public.organizations();
request.B.apiVersion = "v2";
request.execute(function (response) {
console.log(response);
});
Edit 2: Everything seems to be back to normal now.
The another workaround can be done by passing discovery document url.
Sample discovery document url is
url =
http://[application-id].appspot.com/_ah/api/discovery/v1/apis/[endpoint-api-name]/v1/rest
localHostURL =
http://localhost:8080/_ah/api/discovery/v1/apis/[endpoint-api-name]/v1/rest
Example :-
window.gapi.client.load("http://localhost:8080/_ah/api/discovery/v1/apis/[endpoint-api-name]/v1/rest").then(() => { Your promise return callback function })
or
window.gapi.client.load(" http://[application-id].appspot.com/_ah/api/discovery/v1/apis/[endpoint-api-name]/v1/rest").then(() => { Your promise return callback function })

What is the best/proper configuration? (javascript SOAP)

I need to retrieve data from a web service (via SOAP) during a nightly maintenance process on a LAMP server. This data then gets applied to a database. My research has returned many options and I think I have lost sight of the forest for the trees; partially because of the mix of client and server terms and perspectives of the articles I have read.
Initially I installed node.js and node-soap. I wrote a simple script to test functionality:
var soap = require('/usr/local/lib/node_modules/npm/node_modules/soap');
var url = "https://api.authorize.net/soap/v1/Service.asmx?WSDL";
soap.createClient(url, function(err, client)
{
if(typeof client == 'undefined')
{
console.log(err);
return;
}
console.log('created');
});
This uses a demo SOAP source and it works just fine. But when I use the actual URL I get a 5023 error:
[Error: Invalid WSDL URL: https://*****.*****.com:999/SeniorSystemsWS/DataExportService.asmx?WSDL
Code: 503
Response Body: <html><body><b>Http/1.1 Service Unavailable</b></body> </html>]
Accessing this URL from a browser returns a proper WSDL definition. I am told by the provider that the 503 is due to a same-origin policy violation. Next, I researched adding CORS to node.js. This triggered my stepping back and asking the question: Am I in the right forest? I'm not sure. So, I am looking for a command-line, SOAP capable, CORS app (or equivalent) configuration. I am a web developer primarily using PHP and Javascript, so Javascript is where I turned first, but that is not a requirement. Ideas? Or, is there a solution to the current script error (the best I think I have found is using jQuery in node.js which includes CORS)
Most likely, this error belongs to your website server.
Please go through this link, it might be helpful.
http://pcsupport.about.com/od/findbyerrormessage/a/503error.htm
Also you can open your wsdl in web browser, search for soap:address location tag under services. And figure out correct url, you are trying to invoke from your script. Directly access this url in browser and see what are you getting.
I think I have a better approach to the task. I found over the weekend that PHP has a full SOAP client. I wrote the same basic login script in PHP and it runs just fine. I get a valid authentication code in the response to loginExt (which is required in further requests), so it looks like things are working. I will comment here after verifying that I can actually use the web service.

undefined response , facebook javascript SDK

I am trying to import some Photos from a Facebook page that I own.
I am following this answer on Stack Overflow, more specifically the Client-Side part.
In the answer 3 steps are suggested.
Add the javascript SDK , which i do.
Something about Authentication but the link is wrong...
A piece of code for rendering the photos.
I skipped the 2nd step , cause I am not sure what to do there and I implemented the code in 3rd step :
FB.api('593959083958735/photos', function(response) {
if(!response || response.error) {
// render error
alert("Noo!!");
} else {
// render photos
alert("Yeah! " + response.status);
}
From here I get the alert "Yeah! undefined". The response is always undefined. I think maybe because I should have done something in the authentication part.
All I am trying to do here , is to import some photos from a public Facebook page. Is this the correct way to do that? If yes why would I need any authentication for it. And what exactly should I do in the authentication part?
From here I get the alert "Yeah! undefined". The response is always undefined. I think maybe because I should have done something in the authentication part.
You are querying the photos from a page, so there is no status field in the response.
All I am trying to do here , is to import some photos from a public Facebook page. Is this the correct way to do that? If yes why would I need any authentication for it.
You don’t need authentication for that – as you can see here in the Graph API Explorer, you get results even without an access token (after clearing the field). And you can see the structure of the response there as well.

syntax error when trying to use backbone.js with Facebook json feed

I have a super simple Backbone model/collection that wraps around a facebook feed.
window.Story = Backbone.Model.extend({});
window.Stories = Backbone.Collection.extend({
model: Story,
url: 'https://www.facebook.com/feeds/page.php?id=186424828078649&format=json&callback=?',
parse: function(response) {
console.log(response);
return response.entries;
}
});
stories = new Stories();
stories.fetch();
This results in a syntax exception in Chrome ("Unexpected token :"), or in Firefox, an invalid label "title": {.
The Chrome message is a little cryptic, but the Firefox message seems to indicate that the JSON response is being returned. the parse() function is never getting called (as far as I can tell, I've set a breakpoint and added a console.log statement and I'm not seeing anything), so is there anything else that Backbone needs to know in order to parse JSON?
My first thought was that it might be a Cross-Domain issue, but I'm using &callback=? which should make sure that $.ajax uses jsonp. I'm betting that this is working, because Firefox is identifying the 2nd line correctly as "title": {.
Any ideas?
PS, jsfiddle is at http://jsfiddle.net/KcE9L/ if you'd like to test things out.
I figured it out after some deep debugging. It's because Facebook ignores/doesn't support JSONP, and leaves me with this problem: Jquery success function not firing using JSONP. Now I need to figure out how to get CORS working with Facebook, so if anybody can help me along those lines, I'd be curious.

Categories

Resources