Facebook oauth reply with improperly formatted URL parameter - facebook

I am trying to authenticate my Facebook app against a user.
My application provides the following for starting the oauth authorization process:
https://graph.facebook.com/oauth/authorize?state=abc&response_type=code&client_id=292634984182123&redirect_uri=http://dev.foo.com:12020/app/c-rex/authorize-facebook-action/&scope=publish_pages,email&type=user_agent
Then the browser redirects to Facebook, asks for the permissions and redirects to the given redirection URL but with improper URL parameters:
http://dev.foo.com:12020/app/c-rex/authorize-facebook-action/?#state=abc&access_token=wqeqeqBAJmOBc25oYf64IaJBO8Y7k0pOgfo3q3PzoystyqvGPFeve5F8RxZCWtAVUyZB14YaWaZC4escFL69UxcZCHZBI2riqx88rUdKFx7riLPY83D1uY1Qtf9LJfZBuC1nv7sJH1BmIOXV0ZAP94jiaXV5M5tNU99lucTAZDZD&expires_in=6078
As you can see the query string is empty and instead the query strings after the '#' as URL fragment. It looks pretty weird...The URL would be correct if it would not contain the hashmark. Since fragment is irrelevant for the server I can not pick up the fragment from the URL inside my code.
Is this a Facebook bug?

https://developers.facebook.com/docs/apps/changelog
[Oauth Access Token] Format - The response format of https://www.facebook.com/v2.3/oauth/access_token returned when you exchange a code for an access_token now return valid JSON instead of being URL encoded. The new format of this response is {"access_token": {TOKEN}, "token_type":{TYPE}, "expires_in":{TIME}}. We made this update to be compliant with section 5.1 of RFC 6749.

Related

How to convert api body x-www-form-urlencoded to raw plain/text

I'm setting up oAuth generation in new testing software, Tosca. Tosca can only handle API calls with raw body types. I need to convert below x-www-form-urlencoded payload to plain/text. The request is denied with "developerMessage": "The authentication service denied the request".
I have tried changing all of the ":"'s to "=" and all linebreaks to "&." Which has worked for converting other x-www-form-urlencoded payloads to plain/text. I have removed our client_id and client_secret and added them to basic auth. I have removed any and all special characters and spaces from the body and still getting error.
scope:openid
realm:employer
auth_method:cleartrust
auth_id_user_token:A+B/C/D==
nonce:cd
identity_method:air
response_type:id_token token
grant_type:password
which I changed to
scope=openid&realm=employer&auth_method=cleartrust&auth_id_user_token=A+B/C/D==&nonce=cd&identity_method=air&response_type=id_token token&grant_type=password
The plain/text api call should get a successful response and bring back oAuth token.
Full disclosure: I work for Tricentis in the Support Department.
Tosca's API Engine can also handle url-encoded payloads. See this manual page for an example screenshot: https://support.tricentis.com/community/manuals_detail.do?lang=en&version=12.3.0&url=engines_3.0/api/api_create_url-encoded.htm
Additionally, please follow these steps:
In the Technical View tab, open the Headers pane on the right-hand side. There enter a Content-Type header param with value application/x-www-form-urlencoded.
In the Auth tab, select Basic Auth as the method and use the client_id as the username and the client_secret as the password. Tick the checkbox Pre-authenticate.
In the Payload tab, you can enter the url-encoded payload required by your web service. Since you use grant_type = password, the payload has to include at least the string grant_type=password&username=someUsername&password=somePassword

did facebook oauth/authorize get deprecated?

I have been authenticating user against facebook using an api call like
https://graph.facebook.com/oauth/authorize?client_id=715408518535970&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Foauth%2Fecho&scope=email%2Cread_stream
this would redirect and there would be an access_token query parameter.
Now during the redirect, there is only a query parameter called code.
Has this method of authenticating been deprecated?
Yes it is deprecated.
https://developers.facebook.com/docs/apps/changelog
[Oauth Access Token] Format - The response format of https://www.facebook.com/v2.3/oauth/access_token returned when you exchange a code for an access_token now return valid JSON instead of being URL encoded. The new format of this response is {"access_token": {TOKEN}, "token_type":{TYPE}, "expires_in":{TIME}}. We made this update to be compliant with section 5.1 of RFC 6749.

Linkedin OAuth2 authorization code error

I´m trying to connect via Linkedin Auth2 from a java web application:
Added my own app in linkedin.
Generate the authorization URL: https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=XXX&scope=r_basicprofile%20r_fullprofile%20r_emailaddress&state=DCEEFWF454Us5dffef424&redirect_uri=http://localhost:9090/springmvc/token.htm
Introduce my login/password for linkedin in the new popup.
Get back successful the request on the redirect_uri previus, and take the authorization code "code"
Generate the accessToken URL
Make a POST with: https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=YYY&redirect_uri=http://localhost:9090/SpringMVC/token.htm&client_id=XXX&client_secret=ZZZ
Get next error in response: {"error_description":"missing required parameters, includes an invalid parameter value, parameter more than once. : Unable to retrieve access token : appId or redirect uri does not match authorization code or authorization code expired","error":"invalid_request"}
I´ve verified url parameters are correct:
- "code" is the token receive on step 4.
"redirect_uri" is the same URL on step 2 and 4. I added this URL on section "OAuth 2.0 redirect URL" on my APP.
"client_id" and "client_secret" are the "Client API" and "Secret API" on my APP.
Time between first and second request is less than 20 seconds.
The value of your redirect_uri parameter must be URL-encoded, so at 6. do:
https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=YYY&redirect_uri=http%3A%2F%2Flocalhost%3A9090%2FSpringMVC%2Ftoken.htm&client_id=XXX&client_secret=ZZZ
and it need not be sent as a POST but as a GET.
If You Are Sending a Request For Access Token it must be POST Request
Refer the OAuth Documentation
I actually Copied the Whole URL From My Eclipse Console To URL It is Still Valid
In Your Case The Problem is with URL Encoding As HanZ said .
You Have to Encode Your URL For Post Request.
I too got bugged with this issue for long time. Please keep few things in mind which I did and eventually sorted it out.
Hit the api to get authorization code by using get request.
The authorization code has a life span of about 20 seconds, so its difficult to manually copy the code from the url and make a request for token access. You should do it pro-grammatically.
Make a post request for getting access token.
Most Important: Old linkedin applications that I had created was not working and giving the above error. Create a new linkedin application and try. Doing this worked for me.
I assume that all the other parameters like client_id, secret, redirect_uri are correct.
please try and let us know.
Thanks.

Facebook server-side authentication flow: is this the right "code?"

I'm using FB.login on the JS client and want to verify the user's identity on the server. So, the client gets a signedRequest from facebook and sends it to the server. The server splits on the period, and decodes the second part of the signedRequest into a json object.
What should I be using for "code" when I send my server-side request to
https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID
&redirect_uri=YOUR_REDIRECT_URI
&client_secret=YOUR_APP_SECRET
&code=CODE_GENERATED_BY_FACEBOOK
My decoded json looks something like:
{"algorithm":"HMAC-SHA256","code":"2.AQCPA_yfx4JHpufjP.3600.1335646800.1-5702286|l11asGeDQTMo3MrMx3SC0PksALj6g","issued_at":1335642445,"user_id":"5232286"}
Is that the code I need? Does it need to be B64 encoded? If this isn't the code, what code should I use?
_
What I've tried:
The request I'm trying to use is:
https://graph.facebook.com/oauth/access_token?client_id=295410083869479&redirect_uri=https://squaredme.appspot.com/facebookredirect&client_secret=44f1TOPSECRETbb8e&code=2.AQCPA_yfx4JHpufjP.3600.1335646800.1-5702286|l11asGeDQTMo3MrMx3SC0PksALj6g
but this returns the error:
{"error":{"message":"Error validating verification code.","type":"OAuthException","code":100}}
I can't tell if this is because I'm using a bad code, or what. Noteably, this is running on my local dev server, and squaredme.appspot.com definitely does NOT resolve to my IP. I don't know if facebook checks that or what - I'm assuming I'd get a better error message. Thanks for any direction!
You are trying to somehow combine the two flows together and that's why things don't work well.
When facebook POSTs into the iframe with your app url and a signed request there are two options, the easy one being that the user is already authenticated and then the signed request will have all the necessary data (including a signed request), then you just load the canvas page and use the JS SDK to get an access token there as well, but in this case there's no need to use the FB.login (since it opens a popup and will automatically close it), you can use the FB.getLoginStatus method which won't annoy the user.
If the user is not authenticated then the sign request will be missing the things you need to use the graph api.
You then redirect the user to the auth dialog, and since you are loaded in an iframe you'll need to return a html response which redirects the parent window using javascript, like:
top.location.href = "AUTH_DIALOG_URL";
When the use is done (accepted or rejected the app) he will be redirected to the "redirect_uri" you added as a parameter to the auth dialog.
If the user accepted your app then you'll be getting the "code" parameter in the query string.
You then take the code, exchange it with an access token as you posted in your question, and then redirect the user back to "apps.facebook.com/YOUR_APP".
When the page then loads the user is already authenticated and you'll be getting a full signed request.
I hope this clarifies things for you, recheck the Server-Side flow it pretty much covers it all.
I also had some trouble with that, then I found the solution here in StackOverflow.
There are two kinds of "code" provided by facebook. One comes inside the signedRequest in the cookie generated by the client-side flow. The Facebook's JS SDK handles this codes and get a access token without telling us anything.
The other type of code comes attached as a query to your redirect URI (http://www.yoururl.com/index.php?code=AAAgyiaus...), when you navigate to OAuth URL (server-side flow). With this code, you go to a Token URL and get your access token.
When you are using the server-side flow, you need to indicate a redirect URI both in the OAuth URL AND in the Token URL, and they have to be exactly the same, so a missing slash or a query string can be a lot of problem.
The codes are different from each other. When you use the both things together, appears to be impossible to get a access token using the code that was inside the cookie's signedRequest.
BUT, it is not. The magic is: the code from signedRequest is associated with NO URI, so as long as the redirect_uri is a mandatory field, all you have to do is to pass it blank when you navigate to the Token URL.
So the final solution is: grab the signedRequest from the cookie, parse it in your server to obtain the code, then read the Token URL:
https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID
&redirect_uri=&client_secret=YOUR_APP_SECRET
&code=CODE_INSIDE_THE_SIGNED_REQUEST
It looks like a hack, so I don't know how long it's gonna work, but it's working right now.

Parse facebook code param

I'm tring to authenticate with facebook using the following request:
https://www.facebook.com/dialog/oauth?client_id=MYAPPID&redirect_uri=http://localhost:3000/oauth/callback.html&scope=email
The response is the following:
http://localhost:3000/oauth/callback.html?code=AQA6VzXu_In9_GIiqu-GFEo6d8sA4jM5L6rLQWtL9g2aMo2Ju5h9j_uCcqR-w7cYifyi0IYsOHtOk5S_jKBBlgQatybYDHOfSs2EpA3H3NHQIDIaKmC-9kje9_QQbhPd0Ge1pP-52OR7iOQWc_R8D-YapXcArTAmpIHLBHatOSHB0x3lFv4DVUECfb1IdYIZlVM#_=_
The problem is that I'm unable to retrieve an access token from this code. It should normally have a dot delimiter (".") whereas this has none, that's why I'm unable to decode it.
Any ideas on what I'm doing wrong?
You shouldn'T be able to decode it, it's not a signed_request.
You need to send a request to
https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&
client_secret=YOUR_APP_SECRET&code=THE_CODE_YOU_RECEIVED
in order to get an access token
There is a difference between CODE and TOKEN, both are very widely confused.
Client-side login (Javascript + HTML)
If you want to use javascript only (work with HTML only) as it is probably your case you should add &response_type=token to your first request.
Also, I don't think you actually need to split and work with the token but give the JS api to do it for you.
Server-side login (PHP)
You know have a user that a accessed your page with the CODE.
Now you must access, with your server-side script, this url and it will return the access_token.
https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&
client_secret=YOUR_APP_SECRET&code=CODE
If your callback was a PHP you could just filegetcontents along with json_decode but I recommend using the official Facebook SDK.
Highly recommended doc page -
http://developers.facebook.com/docs/authentication/