我正在尝试代表经过身份验证的用户访问Google Analytics数据。
到目前为止,我已成功使用Socialite(OAuth 2.0)将用户登录,然后将其“令牌”存储在回调函数中的会话中。
然后我尝试使用Google的PHP库来访问他们的分析数据。但是,我收到以下错误:
The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved.
以下是我尝试使用的代码(换行符和注释以便于阅读):
// New up google client from include PHP library
$client = new Google_Client();
// Set credentials createdin Google Developer Console
$client->setClientId(env('GOOGLE_CLIENT_ID'));
$client->setClientSecret(env('GOOGLE_SECRET_ID'));
// Add scopes for access
$client->addScope('https://www.googleapis.com/auth/plus.profile.emails.read');
$client->addScope('https://www.googleapis.com/auth/analytics.readonly');
$client->addScope('https://www.googleapis.com/auth/userinfo.email');
// Apply authenticated user's token that was returned by Google
$client->setAccessToken(json_encode(array('access_token' => Auth::user()->token)));
// Now new up a service and pass in the above client
$analytics = new Google_Service_Analytics($client);
// Try to get users' accounts and dump, but this causes the error
// Note: any call to the service will cause the same error
dd($analytics->management_accounts->listManagementAccounts());
我还提取了用户代码,在传入新服务之前尝试:$client->authenticate(Auth::user()->code);
。
但正如预期的那样,该代码是“一次性使用”并且已经被使用过。
在通过Socialite(Laravel 5.2)对用户进行身份验证后,是否有人建议访问任何Google API?
非常感谢您阅读!!