Bug in Bing Webmaster Tools OAuth 2.0?

RA van Hagen 5 Reputation points
2025-12-12T19:35:12.8133333+00:00

Hi, while creating a web app that uses the Bing Webmaster Tools API with OAuth 2.0 credentials, I ran into several issues.

  1. The documentation on https://learn.microsoft.com/en-us/bingwebmaster/oauth2 seems to have some errors.
  2. The common assumption (on these forums) that refresh tokens are one-time use seems not to be the case.
  3. The app receives a new refresh token each time when refreshing an expired access token. But using this new refresh token (on the next access token expiration) will cause a invalid_grant error response. This seems to go against the OAuth 2 specs.

What I expected was this:

The app, after the first authorization request, obtains the first access and refresh tokens. By the time the access token expires (one hour, I noticed) the app uses the refresh token to request a new access token. And if a new refresh token is received with the request, the app replaces the old refresh token with the new one for future refresh requests.

But...

I found that the app was getting invalid_grant responses as soon as it uses the second refresh token. But if I make the app ignore these new refresh tokens, and keep using the original refresh token, it does obtain a new access token each time.

Or at least, today. No idea when the refresh token will expire...

Can anyone explain why Bing sends a new refresh token on each access token refresh? And why these new refresh tokens cause an invalid grant response?

Should the app save these refresh tokens for later use but only use them after the original one has expired or something? Feels messy...

Bing | Bing Webmaster Tools
Bing | Bing Webmaster Tools
A Microsoft service that provides website owners with tools to monitor, analyze, and optimize their site's performance in Bing search results.
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2025-12-12T19:35:33.9733333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    It seems you are encountering issues with the refresh tokens in the Bing Webmaster Tools API's OAuth 2.0 implementation. Based on the context provided, here are some insights:

    1. Refresh Token Behavior: According to the OAuth 2.0 specification, when a new refresh token is issued, the old refresh token should be discarded. This means that your application should replace the old refresh token with the new one each time it is received. However, if you are receiving an invalid_grant error when trying to use the new refresh token, it indicates that there may be an issue with how the refresh token is being handled or validated.
    2. Invalid Grant Error: The invalid_grant error can occur for several reasons, including using a refresh token that has expired, been revoked, or is not valid for the requested scope. If the new refresh token is causing this error, it might be due to the token being invalidated by the authorization server for some reason, such as a change in user consent or other security measures.
    3. Token Expiration: Refresh tokens are generally long-lived, but they can be invalidated at any moment. Therefore, your application should be designed to handle the possibility that a refresh token may no longer be valid when you attempt to use it.
    4. Best Practices: It is advisable to always use the most recent refresh token received after a successful token refresh request. If you receive a new refresh token, you should store it and discard the old one. If you encounter an invalid_grant error, you may need to prompt the user to re-authenticate to obtain a new set of tokens.

    In summary, while it may seem messy, the behavior you are observing aligns with the OAuth 2.0 specification regarding refresh tokens. It's important to handle token management carefully to ensure that your application can respond to changes in token validity appropriately.


    References:

    0 comments No comments

  2. Aron 7,500 Reputation points Independent Advisor
    2025-12-12T20:45:21.22+00:00

    Hi there, thanks for sticking with me while we sort this out.

    From what I’m seeing, the issue comes down to how Bing Webmaster Tools handles OAuth refresh tokens: they rotate on each successful refresh, and the previous token becomes invalid, so if the app reuses an old token or two refresh attempts happen close together, Bing returns invalid_grant.

    To get this stable, let’s make sure we’re posting to https://www.bing.com/webmasters/oauth/token with grant_type=refresh_token, and after a successful refresh we immediately and atomically save the new refresh_token and use a single thread/locked path so only one refresh can run at a time.

    Also, double‑check the redirect URI matches exactly, your system clock is accurate, and scopes are consistent; if a refresh still fails, kick off a full re‑auth to get a new code and token pair.

    Quick fallback while we test: you can use the API Key from the Webmaster Tools “API Access” section to keep calls working.

    A couple questions to help me pinpoint this: are multiple instances or jobs trying to refresh at the same time, how are you persisting and swapping the new refresh token, and can you share the exact form body (with secrets removed) you’re sending to the token endpoint?

    Regards,
    Aron


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.