我现在已经找到了解决方案。起初,我认为我需要从谷歌返回身份验证 URL 的代码,当我检查 Socialite 包时,我在 \vendor\laravel\socialite\src\Two\AbstractProvider.php 中找到了一个受保护的方法 getCode(),它从 URL 返回代码。我编辑了包的源文件并将方法类型从protected 更改为public,这样就可以在类之外使用该方法,这使我可以从 URL 访问代码,然后存储它在 DB 中以获取进一步的身份验证要求。但是这个设置存在问题,首先,我应该找到一种方法来保留该包而不进行任何更新,因为任何更新都会回滚我对源文件所做的更改。我面临的第二个问题是我存储令牌的方式。默认情况下,Google 客户端 API 返回一个包含字段 access_token、refresh_token、expires_in、id 和 created 的数组,并使用这些字段验证对分析服务器的请求。在我的场景中,没有从基本社交名流登录过程返回的标准数组。有access_token、refresh_token 和expires 变量,我也将它们全部存储在我的数据库中。这导致了谷歌库的问题,它要求一个结构化数组,我什至没有变量expires_in 和created,这就是为什么我设置一个假数组告诉谷歌在每个请求时刷新令牌,而这个也不是一个好习惯。
最后,我无法理解如何在线使用任何包,我自己编写了简单的身份验证,我不知道它是否有任何漏洞,但它对我有用,它也可能对那些需要的人有用它。
这是我的路线:
Route::get('auth/google', [
'as' => 'googleLogin',
'uses' => 'Auth\AuthController@redirectToProvider'
]);
Route::get('auth/google/callback', [
'as' => 'googleLoginCallback',
'uses' => 'Auth\AuthController@handleProviderCallback'
]);
这些是AuthController 方法:
/**
* Redirect the user to the Google authentication
*/
public function redirectToProvider()
{
// Create the client object and set the authorization configuration from JSON file.
$client = new Google_Client();
$client->setAuthConfig('/home/vagrant/Analytics/client_secret.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/auth/google/callback');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
$client->addScope("email");
$client->addScope("profile");
$client->setAccessType("offline");
$auth_url = $client->createAuthUrl();
return redirect($auth_url);
}
/**
* Obtain the user information from Google.
*
* @return redirect to the app.
*/
public function handleProviderCallback()
{
// Handle authorization flow from the server.
if (! isset($_GET['code'])) {
return redirect('auth/google');
} else {
// Authenticate the client, and get required informations.
$client = new Google_Client();
$client->setAuthConfig('/home/vagrant/Analytics/client_secret.json');
$client->authenticate($_GET['code']);
// Store the tokens in the session.
Session::put('token', $client->getAccessToken());
$service = new Google_Service_Oauth2($client);
$userInfo = $service->userinfo->get();
$user = User::where('googleID', $userInfo->id)->first();
// If no match, register the user.
if(!$user) {
$user = new User;
$user->name = $userInfo->name;
$user->googleID = $userInfo->id;
$user->email = $userInfo->email;
$user->refreshToken = $client->getRefreshToken();
$user->code = $_GET['code'];
$user->save();
}
Auth::login($user);
return redirect('/home');
}
}
我已经将我从 Google API 控制台下载的client_secret.json 文件放到了指定的文件夹中,这对你来说可能会有所不同。我还修改了迁移文件以匹配所需的段。完成这些步骤后,我可以将该用户视为使用基本 Laravel 身份验证注册的简单用户。
现在我可以像这样查询用户的 Google Analytics(分析)帐户中的帐户:
/**
* @var $client to be authorized by Google.
*/
private $client;
/**
* @var $analytics Analytics object to be used.
*/
private $analytics;
public function __construct()
{
$this->client = $this->AuthenticateCurrentClient();
$this->analytics = new Google_Service_Analytics($this->client);
}
private function AuthenticateCurrentClient(){
$user = Auth::user();
$token = Session::get('token');
// Authenticate the client.
$client = new Google_Client();
$client->setAccessToken($token);
$client->authenticate($user->code);
return $client;
}
public function GetAccounts(){
try {
$accountsObject = $this->analytics->management_accounts->listManagementAccounts();
$accounts = $accountsObject->getItems();
return $accounts;
} catch (apiServiceException $e) {
print 'There was an Analytics API service error '
. $e->getCode() . ':' . $e->getMessage();
} catch (apiException $e) {
print 'There was a general API error '
. $e->getCode() . ':' . $e->getMessage();
}
}
Stack Overflow 帮助了我数千次,我希望这可以帮助某人让事情正常进行。