Google+ OAuth API store and retrieve tokens after first login and authorization

前端 未结 2 593
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 04:00

I have read the documentation, examples and tutorials of how to use the Google API, I have a mini-app running already that shows your latest activities and information, but

2条回答
  •  青春惊慌失措
    2021-01-03 04:27

    This is an old question but it seems to me the answer was not complete.

    The accepted answer works in a way that the user does go through the Google Auth server, just don't see the Auth screen. The question was about storing the token and use it again without sending the user to the Google Server.

    So if that's what you want to do (and it will also allow you to access user data even when they are not currently using your app), all you need to do is ask for an access token that includes a refresh token.

    You do this by using offline access type (which by the way is not the default anymore) - for example in php: $client->setAccessType("offline");.

    Just keep in mind that the access token you receive will include the refresh token only in the first initial authorization by the user, so that's what you need to store.

    Then you can just use that access token with the client, even when it is expired, and the client will take care of refreshing it and getting a new one.

    Hope that helps, Amos

提交回复
热议问题