Problems updating the access token

0

I am authenticating to the Google Calendar API using Scribe, which once uses OAuth 2.0. Authentication completes successfully but I do not know how to store the access token in the database. I'm doing the following:

/*Obter o API USER TOKEN e o API USE SECRET*/
Verifier verifier = new Verifier(code);
Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
API_USER_TOKEN = accessToken.getToken();
API_USER_SECRET = accessToken.getSecret();
/*Guardar tudo na base de dados*/
saveToken(API_USER_TOKEN, API_USER_SECRET);

Then I need to fetch the token and use it again and this is where the problems appear, how do I re-build the token I saved in the database? And how do I update the token?

To update the token I am trying to use the following algorithm:

OAuthRequest request = new OAuthRequest(Verb.POST,"https://www.googleapis.com/oauth2/v3/token");
JSONObject payload = new JSONObject();
request.addQuerystringParameter("grant_type", "refresh_token");
request.addQuerystringParameter("refresh_token", oldToken.getToken());
request.addQuerystringParameter("client_id", API_APP_KEY);
request.addQuerystringParameter("client_secret", API_APP_SECRET);
service.signRequest(oldToken, request);
Response response = request.send();

But I always get the same error (google api response): invalid_grant Bad Request

    
asked by anonymous 07.12.2014 / 02:34

1 answer

2

Your error is in this line:

request.addQuerystringParameter("refresh_token", oldToken.getToken());

The token you need to use to generate a new pair is the refresh token, not the access token.

    
07.12.2014 / 14:49