[Java] How do I refresh an access token using AuthorizationCodeCredential (or maybe there's another class)?

blam 36 Reputation points

I'm looking at this code that fetches an access token to create a GraphServiceClient
How do I refresh an access token in a similar fashion? I already have the refresh token, but the AuthorizationCodeCredential class doesn't seem to allow setting one.


Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,392 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Vicky Kumar (Mindtree Consulting PVT LTD) 1,166 Reputation points Microsoft Employee

    Azure AD authorization code flow requires user interaction to authenticate the user and get Authorization code using /authorize endpoint.
    AuthorizationCodeCredential require prefetched authorization code which you can get from the query parameter in redirect URL specified while calling /authorize endpoint.

    You can extract that query parameter and pass in AuthorizationCodeCredential to get the access and refresh token.

    Authorization Code is a temporary code that is valid for a very short time and is intended only for receiving an AccessToken and RefreshToken. There is no need to store it. If you need to get new access token after access token expire, you don’t need code . You can use refresh token this time to get active access token.

    Refer https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow for authorize endpoint and code and tokens formats.