Refresh Google API access token with Expo SDK? - javascript

I've followed the guide Google login / Expo and got both access token and refresh token.
But after access token expiring I can't get the new token.
When I try to get new one I get this error:
"error": "unauthorized_client", "error_description": "Unauthorized"
Here is the sample of the sent request for getting the new access token (Google docs):
POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded
client_id=<your_client_id>&
client_secret=<your_client_secret>&
refresh_token=<refresh_token>&
grant_type=refresh_token
P.S. I did not forget to replace client_id and other relevant data.

The same client ID that got the refresh token must be used to get another access token!
You are probably using this code on Android, for mobile devices Google doesn't give you a secret since it is not needed. You can remove the secret from the request and change the client ID to match your mobile client ID.

Related

Get the api oath access token from reddit api

I got the code successfully from the reddit oauth api but I am getting invalid grant(Before I was getting 401 unauthorized on sending data as json) error when trying to get the access token after that. I am using postman for sending the post request to https://www.reddit.com/api/v1/access_token Here is My header and form data which I am using in postman. I am sending my client id and secret in authorization tab(tried in sending header) from postman and using x-www-form-urlencoded to send grant type, code and redirect uri(tried sending them as a form data and json as well). In headers my content type is x-www-form-urlencoded. Please help in getting the token if someone have used reddit api.
//Headers
Content-Type:application/x-www-form-urlencoded
//authorization
Authorization:Basic Base.Encode64(clientid:secret)
//client id and secret are those which I got by creating the app in reddit
//x-www-form-urlencoded.
{
"grant_type":"authorization_code",
"code":"authcode which I got from the get request before",
"redirect_uri":"http://localhost:3000"
}
//I tried sending these through query string as well
Your token expires after 3600 minutes if I am not mistaken

Microsoft Oauth2 (token endpoint) - invalid client error

I am trying to get offline access to the outlook calendar from a webview.
(using the authorization_code oauth flow)
currently im doing the following
the javascript website calls the
https://login.microsoftonline.com/common/oauth2/v2.0/authorize
endpoint. Then I set the redirect URI to my spring endpoint.
The spring service is getting a valid code/state, so I assume I did everything correct until now.
Then I am calling the /token endpoint with the following parameters
{
grant_type: authorization_code,
client_id: <client_id>,
client_secret: client_secret,
redirect_uri: <THE SAME AS USED BY AUTHENTICATE>,
scope: https://graph.microsoft.com/mail.read
}
I am getting the following error back:
{
"error" : "invalid_client",
"error_description" : "The OAuth client was not found."
}
I made sure twice that the client secret/id is set correct. I used the microsoft registration portal to create my application, and Web as platform. Live SDK support is also enabled.
https://apps.dev.microsoft.com
Does anyone have suggestions why this doesnt work?
You need to pass back the authorization code you received from the first call in the body of your second call. The following are the parameters you need to pass back:
grant_type - Should be authorization_code
code - The auth code you received
client_id - This is your Application/Client ID
client_secret - This is the Password/Client Secret
scope - This should match the same set of scopes you initial requested
redirect_uri - This is the redirect URI defined in your applicationĀ registration
Also keep in mind that the POST should encoded as application/x-www-form-urlencoded rather than application/json.
I wrote an primer on the v2 Endpoint that you might find useful here as well.

Google OAuth 0.2 Invalid Grant

I Had the following code
$client->setClientId('39605174446-s067746s1jur731n49fujigfh8occ5pa.apps.googleusercontent.com');
$client->setClientSecret('MYCLIENTSECRET');
$client->setRedirectUri('http://examplesss.com/oauth2');
$client->setScopes('https://picasaweb.google.com/data');
$client->setAccessType("offline");
I did authenticate
At my Google Console API, I set it for "Web Application", Authorization Javascript origins I leave it blank.
Authorization Redirect URI I set it as
http://example.com/oauth2
I went to the following site to get my authorisation code
https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=&client_id=39605174446-s067746s1jur731n49fujigfh8occ5pa.apps.googleusercontent.com&redirect_uri=http://example.com/oauth2&response_type=code&scope=https://picasaweb.google.com/data&state=state
When I went into the site, I press ok to grant "offline access" . After that I am redirect to an none existent site which I use that is example.com and then the URI was something like
http://example.com/oauth2?state=stat&code=4/qWt9q19VtilG6Iw6HFte4RnpiXR0a5q80_zQAU-hNqc#
$client->authenticate('4/qWt9q19VtilG6Iw6HFte4RnpiXR0a5q80_zQAU-hNqc#');
Then I get my refresh token code
$client->refreshToken('1\/vYnZlRcvaY2nD0yh5LhU9paLZZggMArGOo-3rEJHGCM');
But when I run the code it say...
PHP Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{
"error" : "invalid_grant"
Wonder what went wrong, I just want to access my picasa web album.
Thanks
It can be 2 things. One, is the incorrect use of the versions of the Google API client or the one I think that is the problem, it's on the refresh token and how you're passing the token. It's a problem with the / character in the string, json enconding and how it's passed. Put the refresh token in double quotes and send the variable. If all the data is correct and authorized via Google, it should work.

Comment on Facebook page posts as page owner with extended token

I'm developing a node.js app which manages a Facebook page among other functionalities.
To comment on user submitted posts, I use the following code in node.js:
FB.api(object + '/comments','post',
{ message: COMMENT_I_WANT_TO_SUBMIT, access_token: MY_PAGE_ACCESS_TOKEN },
function(res){
// deal with res
}
);
When using my short-lived Facebook page access-token (which I get from Graph API Explorer) the comment submitted is shown as a comment submitted by the page itself (what I want), but when I use my permanent token it is shown as submitted by myself (page owner).
To get this permanent token I followed these instructions:
https://stackoverflow.com/a/28418469/4713311
The token has my name associated to it since I had to grant permissions to manage the pages I own to generate the token.
I believe this token has the proper permissions since the token debugger shows the following permissions in scope: "manage_pages, publish_pages, publish_actions, public_profile"
Is there any way I can use this never expiring token to comment on posts with the page name instead of my own?
A Page Access Token should have not only "User ID" but also "Profile ID" associated, which wasn't the case for the extended page access token.
A Page Access Token isn't "extendable", to get a permanent page access token I had to request one with a permanent User Access Token, as explained here
Using as short-lived access token I got from Graph API Explorer (Select App -> Get Token -> Select permissions manage_pages, publish_pages, publish_actions and other you may need -> Grab the token that fills the "Access Token" text box) I made the following call to extend the token.
https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id={app-id}&client_secret={app-secret}& fb_exchange_token={short-lived-token}
Using the token returned, you can simply call /me/accounts and copy the page's access_token
This token is a permanent Page Access Token, to confirm you can use the token debugger and verify it has your "User ID" and your page's "Profile ID"

Sending Request 2.0 app-generated request to other than "/me"

I am trying to send app-generated request using either JS SDK or graph API but I am not successful and desperate. Issue is that when I am sending request to currently logged user, everything is ok, but when I try to send to different user (even this user already authorized my app) I receive OAuth exception "#2 User can't send this request: Unknown error", sometimes "Failed to send any app requests".
My app has Request 2.0 enabled and is authorized with publish_stream, read_stream and user_about_me permissions. Same error messages I described are shown even in Graph API explorer.
Exactly same issue is described here using JS SDK:
Sending an App Generated Request with the JavaScript SDK
... and here using Graph API:
Posting app generated apprequest to other facebook users in Java
Please help.
If you want send a request to a user, you should POST to the Graph API '/user_id/apprequests' using an App Access Token (https://developers.facebook.com/docs/authentication/#applogin). This call is to design to be executed server-side as you don't want to share your App Access Token:
curl -F 'access_token=APP_ACCESS_TOKEN' \
-F 'message=From the app to the user.' \
https://graph.facebook.com/USER_ID/apprequests
The same can be achieve using the User Access Token from the recipient (USER_ID) only:
curl -F 'access_token=USER_ACCESS_TOKEN' \
-F 'message=From the app to the user.' \
https://graph.facebook.com/USER_ID/apprequests
In the JS SDK, if no Access Token field is added, the default Access Token will be of the logged in user. Knowing that, the above call which send the a request to the logged in user, will be:
FB.api('/me/apprequests', 'POST', {message: 'From the app to the user.'});
The first call can also be done with the JS SDK, by adding the App Access Token in the parameters. However, this is a DO NOT USE case as you don't want to share your App Access token client-side.

Categories

Resources