Tran Quan
Tran Quan

Reputation: 1086

Do I need to refresh the access token when using Microsoft Graph and How to do it?

I'm using Microsoft Graph SDK for my iOS Application.

Do I need to manually refresh the access token when it expired?

The access token I'm talking about is: NXOAuth2AccountStore.sharedStore().accounts[0].accessToken

I have tested that I can still query even the accessToken expired. At the time I first logged in, the expired time is 3600 secs. So, I waited 2 hours, test to get user info, events again and still can get it.

I have dump "accessToken.hasExpired" and "accessToken.expiresAt" to make sure access token is expired

Thanks

* More Details *

I follow the sample here: https://github.com/microsoftgraph/ios-swift-connect-sample

I cannot find any documents about refresh access token on Microsoft Graph: https://graph.microsoft.io/en-us/code-samples-and-sdks

Upvotes: 0

Views: 2761

Answers (2)

prasad nikumbh
prasad nikumbh

Reputation: 175

Use this code whenever you need to refresh the access token. This will act as a patch to predefined code provided in graph sdk and you can extract the token from the method :

+(id)tokenWithResponseBody:(NSString *)theResponseBody tokenType:(NSString *)tokenType;

[MSGraphClient setAuthenticationProvider:AppDel.authentication.authProvider];

_graphClient = [MSGraphClient client];

NSMutableURLRequest * sampleReq = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://graph.microsoft.com/v1.0/me"]];

[_graphClient.authenticationProvider appendAuthenticationHeaders:sampleReq completion:^(NSMutableURLRequest *request, NSError *error){    
    if(error == nil)
    {

    }
    else
    {
        [self showToast:@"" message:@"Failure in refresh 0365 token"];
    }
}];

Upvotes: 0

Caitlin Russell
Caitlin Russell

Reputation: 596

Yes, you need to refresh tokens periodically when using Graph in your application. More detailed documentation is available through Azure AD's site: https://learn.microsoft.com/en-us/azure/active-directory/active-directory-authentication-scenarios

The suggested auth library you are using contains a method for refreshing this token:

@implementation NXOAuth2AuthenticatorRefreshCallback

If I haven't answered your question, could you be more specific about what you are trying to accomplish? Are you able to use an expired token or are you unable to refresh your old one?

Upvotes: 2

Related Questions