Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get refreshToken [To avoid user login everytime in the iOS app]. #44

Closed
MIOSY opened this issue Aug 7, 2017 · 3 comments
Closed
Labels

Comments

@MIOSY
Copy link

MIOSY commented Aug 7, 2017

Hi,
I made successful authentication, now user can able to login in my app. But the problem is once user loggs into the app no need to login again that is my requirement, so for this purpose i went through the document which is provided then i understood that using refreshToken i can get accessToken. But in the document refreshToken details are not mentioned.

Here is the code i used to get refreshToken----->
let auth = GTMAppAuthFetcherAuthorization.init(fromKeychainForName: keyChainForAccount)

if auth?.authState.refreshToken == nil {
// some perform segue methods i will call here
}
else{
auth?.authorizeRequest(nil, delegate: self, didFinish: #selector(LaunchViewController.auth(_:finishedRefreshWithFetcher:error:)))
}

func auth(_ auth: GTMAppAuthFetcherAuthorization, finishedRefreshWithFetcher fetcher: GTMSessionFetcher, error: NSError) {
//we come here if and only if we already have an account
if error._userInfo != nil {
self.displayDebugAlert("Error At line 482 with (error.userInfo)")
} else {
loadAuth(auth)
}
}

func loadAuth(_ auth: GTMAppAuthFetcherAuthorization) {
#if DEBUG
NSLog("fetch auth from google completed")
#endif
loadAccoutWithUserName(auth.userEmail!, password: nil, hostname: "imap.gmail.com", oauth2Token:"(String(describing: auth.authState.lastTokenResponse!.accessToken!))") . ////This line will takes me to the mailcore2 For fetching emails
}

How can i get accessToken Using refreshToken in GTMAppAuth Authorisation..........thanks in advance.

@xaphod
Copy link

xaphod commented Dec 4, 2017

You don't. You don't get use the refreshToken yourself, that's handled for you.
If your app is needing to re-auth every time, then probably you have not set the delegates of GTMAppAuthFetcherAuthorization.authState. The important one to implement is OIDAuthStateChangeDelegate -> in didChangeState:, make sure you save the state to the keychain again. Otherwise, what's happening is that the refresh token is being used automatically, but you are not saving it to disk, so the updated access token is never used.

@xaphod
Copy link

xaphod commented Feb 8, 2018

I just found a bug in my code that caused the accessToken not to get refreshed using the refreshToken: I was manually using the token and not using GTMAppAuth to authorize my URLRequest. The codepath that refreshes the token is in the GTMAppAuth authorizeRequest: method. So, the fix was in the place where I want to use the token, to first call authorizeRequest: on a dummy NSMutableURLRequest that matches the scope of the initial OIDAuthorizationRequest's scope, which in my case is "https://mail.google.com/" because i'm sending mail via gmail.

@StevenEWright
Copy link

Seems like this question has been answered. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants
@WilliamDenniss @StevenEWright @xaphod @MIOSY and others