Adnan karim
Adnan karim

Reputation: 1067

Why Google OAuth Api Refreshes Token?

I am trying to get the Access Token using google oauth 2.0 but whenever i login again it goes through the consent screen process and generates a new token.

  1. Is there any way to get same token until the token expires? or(2)
  2. How to stop google from asking to allow to access scopes, if the user has accepted/allowed once.

This is how i am getting the access token.

Uri.https("accounts.google.com",  '/o/oauth2/auth', {
    'response_type': 'code',
    'client_id': identifier,
    'redirect_uri': '$REDIRECTURL',
    'scope': 'https://www.googleapis.com/auth/youtube.readonly',
  });

then:

response = await http.post("https://oauth2.googleapis.com/token", body: {
    'client_id': identifier,
    'redirect_uri': '$REDIRECTURL',
    'grant_type': 'authorization_code',
    'code': code,
  });

Upvotes: 1

Views: 90

Answers (2)

Martin Zvarík
Martin Zvarík

Reputation: 2479

There are parameters setApprovalPrompt = auto / force

Force will cause that it will ask access scopes every time.

Auto won't ask, but you will not get "refresh_token" if you already received it before, and you only get "access_token" that is valid for 1 hour

Upvotes: 1

madhead
madhead

Reputation: 33441

I'm afraid that's how tokens work: whenever you ask one, you get a fresh one. Just store the access and refresh token (needed to refresh the access token when it expires) on your side and don't initiate the OAuth if it's not needed.

Upvotes: 1

Related Questions