我正在构建一个web应用程序,该应用程序必须与Google联系人API交互,并检索经过身份验证的用户的联系人列表,但我得到了
ClientException in RequestException.php line 89:
Client error response [url] https://www.google.com/m8/feeds/contacts/ambermphatic@gmail.com/full?prettyPrint=false [status code] 403 [reason phrase] Forbidden
这是我的AuthenticateUser.php,我在其中包含了getContactList函数,我正试图向谷歌服务器发出Guzzle请求,并通过将其存储在会话变量中来发送正确的访问令牌,但我仍然得到了禁止的响应:
<?php
namespace App;
use LaravelSocialiteContractsFactory as Socialite;
use AppRepositoriesUserRepository;
use IlluminateContractsAuthGuard;
class AuthenticateUser {
/**
* @var UserRepository
*/
private $users;
/**
* @var Socialite
*/
private $socialite;
/**
* @var Guard
*/
private $guard;
private $token;
public function __construct(UserRepository $users, Socialite $socialite, Guard $guard)
{
$this->users = $users;
$this->socialite = $socialite;
$this->guard = $guard;
}
/**
* @param $hasCode
* @param AuthenticateUserListener $listener
* @return mixed
*/
public function execute($hasCode, AuthenticateUserListener $listener)
{
if ( ! $hasCode ) return $this->getAuthorizationFirst();
$var = $this->getGoogleUser();
$user = $this->users->findByUsernameOrCreate($var);
Session::put('token', $var->token );
Auth::login($user, true);
return $listener->userHasLoggedIn($user);
}
public function logout()
{
Auth::logout();
return redirect('/');
}
private function getAuthorizationFirst()
{
return Socialize::with('google')->redirect();
}
private function getGoogleUser()
{
return Socialize::with('google')->user();
}
public function getContactList()
{
$client = new GuzzleHttpClient();
$email = Auth::user()->email;
$token = Session::get('token');
$json = $client->get('https://www.google.com/m8/feeds/contacts/'. $email . '/full', [
'query' => [
'prettyPrint' => 'false',
],
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $token ,
],
]);
dd($json);
return $json;
}
}
这是我的AuthController.php
<?php namespace AppHttpControllers;
use AppAuthenticateUser;
use AppAuthenticateUserListener;
use AppHttpRequests;
use AppHttpControllersController;
use LaravelSocialiteTwoGoogleProvider as Google;
use IlluminateHttpRequest;
class AuthController extends Controller implements AuthenticateUserListener
{
public function login(AuthenticateUser $authenticateUser, Request $request){
return $authenticateUser->execute($request->has('code'), $this);
}
public function userHasLoggedIn($user)
{
return redirect('/');
}
public function logout(AuthenticateUser $authenticateUser){
return $authenticateUser->logout();
}
public function getContactList(AuthenticateUser $authenticateUser)
{
$response = $authenticateUser->getContactList();
dd($response);
}
}
这是我的MainController.php
<?php namespace AppHttpControllers;
use AppHttpRequests;
use AppHttpControllersController;
use IlluminateHttpRequest;
class MainController extends Controller {
public function index()
{
if (Auth::check()) return redirect('google_welcome');
return redirect('google_login');
}
public function first()
{
return view('google_login');
}
public function back()
{
$user = Auth::user();
return view('google_welcomeback')->with('user', $user);
}
}
我对PHP和Laravel的世界还很陌生,这是关于尝试立即使用Google API和使用oAuth 2的socialite等包的问题。我真的很难充分利用我有限的知识,也没有在网上找到太多文件,问题是我的雇主暗示我要么必须尽快完成,要么他会给我指明出路。。。
我今天遇到了同样的问题。调试Socialite我发现对谷歌的获取授权令牌调用有问题。
最后,它只是错误地粘贴到配置中的客户端机密副本。
要发现问题的真正来源,您可以尝试以下命令:
curl -v -d grant_type=authorization_code -d client_id="<YOUR_ID_HERE>" -d client_secret="<YOUR_SECRET_HERE>" -d code="<CODE_HERE>" -d redirect_uri="<YOUR_CALLBACK_URI_HERE>" https://accounts.google.com/o/oauth2/token
您已经拥有的客户端ids/secrets/uri和代码可以通过调试器或添加一些调试打印项来获得/vendor/laravel/socialite/src/Two/GoogleProvider.php-例如在getAccessToken($code)方法中。
对我来说,它带来了真正的答案:
{
"error" : "invalid_client",
"error_description" : "The OAuth client was not found."
}
一旦你解决了这个问题,还有一个没有记录的警告-Socialite需要你的谷歌应用程序启用Google+API。这可以在以下位置完成:https://console.developers.google.com
我已经设法通过并最终克服了这个错误,将我的getContactList函数更改为以下内容并添加了正确的范围:
public function getContactList()
{
$config = [
'client_id' => env('CLIENT_ID', ''),
'client-secret' => env('CLIENT_SECRET', ''),
];
$client = new GuzzleHttpClient($config);
$email = Auth::user()->email;
$token = Session::get('token');
$json = $client->get('https://www.google.com/m8/feeds/contacts/default/full/', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
dd($json);
return $json;