picasa icon indicating copy to clipboard operation
picasa copied to clipboard

Issue about "Code was already redeemed" in service account process

Open welson327 opened this issue 11 years ago • 5 comments

Dear Sir,

Thanks for your contribution about gem of picasa.

My app is use the way of server to server, such as: https://github.com/morgoth/picasa#permanent-server-side-usage

When following your step, it can work, but only once! Google server always response me as following: //-------------------------------------- Authorization failed. Server message: (Signet::AuthorizationError) { "error": "invalid_grant", "error_description": "Code was already redeemed." } //-------------------------------------- But i'm a web application that any member can upload photo via ONE account i create beforehand. Is anything my step wrong? Thanks.

welson327 avatar Jun 29 '15 02:06 welson327

Just to cover bases:

  1. You have created your own project into Google Developer's Console (console.developers.google.com)
  2. Fetched your client id and client secret from Google Developer's Console
  3. Used your Google account in combination with client Id and client secret to fetch the code
  4. Transformed code into access_token and refresh_token
  5. Stored that refresh_token into config file or something
  6. Use refresh token to acquire access token.

'Code was already redeemed' sounds like something that might happen if access token has already expired. I think the default expiration time for access token is something like 10 minutes, which is why we should be more interested about refresh_token than access token.

jylitalo avatar Jun 29 '15 05:06 jylitalo

@welson327 It looks like you can only exchange code to refresh_token only once, so when you try once again you get error like this. Repeat the steps and note new code from redirected page and then run only once:

signet = Signet::OAuth2::Client.new(
  code: code,
  token_credential_uri: "https://www.googleapis.com/oauth2/v3/token",
  client_id: client_id,
  client_secret: client_secret,
  redirect_uri: "urn:ietf:wg:oauth:2.0:oob"
)
signet.fetch_access_token!
refresh_token = signet.refresh_token

Now you need to store refresh_token and on your server you can repeat this code every time to get fresh access_token

signet = Signet::OAuth2::Client.new(
  client_id: client_id,
  client_secret: client_secret,
  token_credential_uri: "https://www.googleapis.com/oauth2/v3/token",
  refresh_token: refresh_token
)
signet.refresh!

# Use access token with picasa gem
signet.access_token

morgoth avatar Jun 29 '15 06:06 morgoth

Fell free to reopen if you have still issues

morgoth avatar Jul 03 '15 13:07 morgoth

Your solution worked. The key point is run only once to get the authorization code. Thanks for your patience.

Another question is: Is access_token expired? The google official website API says we should run it periodically to avoid token expire. (http://stackoverflow.com/questions/13851157/oauth2-and-google-api-access-token-expiration-time) Any comment about this?

Thanks.^_^

welson327 avatar Jul 08 '15 05:07 welson327

When you made your initial call for the code, you had option for specifying how long each access token is valid. You can also query remaining time with tokeninfo (http://stackoverflow.com/questions/13851157/oauth2-and-google-api-access-token-expiration-time) My personal solution on this issue is that my scripts (you run them, they do something and exit kind of stuff) always assume that there are no valid access tokens, so they always use refresh token to get new access token and use that one.

jylitalo avatar Jul 08 '15 05:07 jylitalo