0

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

Tran Quan
  • 1,056
  • 15
  • 28

2 Answers2

2

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?

Caitlin Russell
  • 596
  • 3
  • 11
  • Hi Caitlin Bales, Thanks for your answer I did not use Microsoft Azure Active Directory. I use Microsoft Graph, I register an application at : https://apps.dev.microsoft.com/Landing I am just wonder because I've tested, and saw the access token is expired, but I still can send quest (for example, get events in calendar). – Tran Quan Dec 10 '16 at 02:07
  • Yes, Microsoft Graph uses Azure AD to authenticate users (converged auth: https://graph.microsoft.io/en-us/docs/authorization/converged_auth). I will look into the expired token issue and create a service bug for the relevant team if necessary. – Caitlin Russell Dec 12 '16 at 22:58
0

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"];
    }
}];
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47