问题
Im trying to figure out Google calendars but I cant make sense out of which string I need to store in my db to fetch access tokens when I need to push or get data.
I make my request to Google and they return:
?code=4/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx#
Then I authenticate:
$client->authenticate($_GET['code']);
Then I get access token:
$client->getAccessToken()
Everything works great for this session. But, I dont want to have to ask the user to authenticate each time. Can I store the $_GET['code'] string and then request an access token without having the user to authenticate each time?
Thanks.
回答1:
Yes, this is absolutely possible.
You will want to read about offline access from the OAuth 2.0 for Web Server Applicaitons. This will prevents user from your application to be prompted with the consent screen for authorization every time.
To use the 'offline access', you will need to apply the following code snippet to your app:
$client->setAccessType("offline");
You may also want to read more about the OAuth 2.0 at this link: https://developers.google.com/identity/protocols/OAuth2
Hope it helps and Good luck!
来源:https://stackoverflow.com/questions/33762065/google-calendar-api-storing-code-or-access-token