我正在构建一个必须与 Google Contacts 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 函数,我正在尝试向 Google 服务器发出 Guzzle 请求,并通过将其存储在会话变量中来设法发送正确的访问令牌,但我仍然被禁止回复 :
<?php
namespace App;
use Laravel\Socialite\Contracts\Factory as Socialite;
use App\Repositories\UserRepository;
use Illuminate\Contracts\Auth\Guard;
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 \GuzzleHttp\Client();
$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 App\Http\Controllers;
use App\AuthenticateUser;
use App\AuthenticateUserListener;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Two\GoogleProvider as Google;
use Illuminate\Http\Request;
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 App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
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 的社交名流)有什么意义。我真的很难充分利用我有限的知识,但还没有真正找到网上有很多文档,问题是我的雇主暗示我要么必须尽快完成这项工作,要么他会告诉我出路...