Repeated authorization code

787 views
Skip to first unread message

Warren Zhang

unread,
May 11, 2017, 3:50:10 PM5/11/17
to AdWords API Forum
Hi,

We've gotten the following exception when trying to generate a token:

{"error" : "invalid_grant", "error_description" : "Code was already redeemed."}

Every time we make a request, we're directly using the authentication code passed via the request sent to our callback. We've seen this error occur 3 times for a single user within about 12 minutes. The user successfully used this same authorization code about 2 hours before these failures. Could we get some assistance in troubleshooting this issue?

Thanks,
Warren

James Andrews

unread,
May 11, 2017, 4:36:43 PM5/11/17
to AdWords API Forum
Warren,

You can only use the authorization code once.  It's one and done.  After the code has been used you need to generate a new authorization code.

James

Shwetha Vastrad (AdWords API Team)

unread,
May 11, 2017, 4:48:35 PM5/11/17
to AdWords API Forum
Hi Warren, 

Yes, as James states, an authorization code can only be used once to get a short-lived access token and a refresh token. You'll not be able to use an authorization code more than once. You'll need to reauthorize to generate a new authorization code. You can use the refresh token to refresh an expired access token as described here.

Regards,
Shwetha, AdWords API Team.

Warren Zhang

unread,
May 11, 2017, 4:56:46 PM5/11/17
to AdWords API Forum
Hi Shwetha,

The issue is not us trying to re-use an authorization code. From our perspective, it looks like we're getting back the same authorization code (which has already been used) after separate Google login attempts.

Thanks,
Warren

Shwetha Vastrad (AdWords API Team)

unread,
May 11, 2017, 5:20:03 PM5/11/17
to AdWords API Forum
Hi Warren,

Could you provide the type of OAuth2 credentials used and also describe how you have implemented OAuth2 authentication in your application?

Thanks,
Shwetha, AdWords API Team.

Warren Zhang

unread,
May 11, 2017, 5:28:19 PM5/11/17
to AdWords API Forum
Hi Shwetha,

This is for a .NET web app. What details do you need? 

Thanks,
Warren

Shwetha Vastrad (AdWords API Team)

unread,
May 12, 2017, 10:18:55 AM5/12/17
to AdWords API Forum
Hi Warren, 

Could you confirm if you followed the steps listed here to setup OAuth2 for API access? Are you saving/caching the authorization code in your application?  An end-to-end example is provided here which demonstrates how to access the AdWords API from within an ASP.NET web application. Could you try using this example and let me know if it works? 

Warren Zhang

unread,
May 15, 2017, 1:33:28 PM5/15/17
to AdWords API Forum
Hi Shwetha,

Yes, we're following the same steps listed in the tutorial provided to obtain an authorization code. We are adding the following arguments to the authorization url - &approval_prompt=force and &prompt=select_account. Not sure if these are having any impact. We're not caching the authorization code anywhere explicitly; we directly fetch it from Request.Params["code"].

Thanks,
Warren

Shwetha Vastrad (AdWords API Team)

unread,
May 15, 2017, 2:24:04 PM5/15/17
to AdWords API Forum
Hi Warren, 

The parameter approval_prompt is now prompt. To get the behavior similar to approval_prompt=force I would suggest that you change your app to use prompt=consent. Please see this forum post for more details. In your case, you would need to pass the space delimited values to the prompt parameter. 

Warren Zhang

unread,
May 15, 2017, 2:59:12 PM5/15/17
to AdWords API Forum
I can change that, but is the outdated parameter the cause of this authorization code issue?

Anash P. Oommen (AdWords API Team)

unread,
May 17, 2017, 11:22:07 AM5/17/17
to AdWords API Forum
Hi Warren,

The relevant code to capture OAuth callback and exchange it with an access token / refresh token looks like this: 
https://github.com/googleads/googleads-dotnet-lib/blob/master/examples/AdWords/CSharp/OAuth/OAuthLogin.aspx.cs#L50. I assume you are (1) using the state parameter to distinguish a redirect to google server, and a callback from Google server? and (2) Placed a breakpoint on your call to FetchAccessAndRefreshTokens and made sure it isn't happening twice?

You can also use Fiddler to intercept the requests to make sure the authorization code in both cases are different (i.e. the call is not getting cached somehow)

Cheers,
Anash P. Oommen,
AdWords API Advisor

Warren Zhang

unread,
May 17, 2017, 1:13:58 PM5/17/17
to AdWords API Forum
Hi Anash,

We're distinguishing between Request.Params["state"] == null and Request.Params["state"] equals something else, but not checking for whether or not that something else == callback. Could this be cause the issue? We've moved where we call FetchAccessAndRefreshTokens from our UI code to our back end code, but it also doesn't look like we call FetchAccessAndRefreshTokens there either. Since this doesn't happen consistently or very often, we've had issues trying to repro the issue, so I'm not sure we'll be able to capture this re-used authorization code callback through Fiddler. 

Thanks,
Warren

Warren Zhang

unread,
May 17, 2017, 3:04:03 PM5/17/17
to AdWords API Forum
Meant to say it doesn't look like we call FetchAccessAndRefreshTokens multiple times there either. 
Reply all
Reply to author
Forward
0 new messages