在Laravel使用Guzzle和Socialite向Google API发出请求5 [英] Making requests to Google API using Guzzle and Socialite in Laravel 5
问题描述
我正在构建一个必须与Google Contacts API交互的Web应用程序,并检索经过身份验证的用户的联系人列表,但我得到了
RequestException.php中的ClientException第89行:
客户端错误响应[url] https://www.google.com/m8/feeds/contacts/ambermphatic@gmail.com/full?prettyPrint = false [status code] 403 [reason phrase]禁止
这里是我的AuthenticateUser.php, getContactList函数,我试图让谷歌服务器的Guzzle请求,并设法通过将它存储在会话变量中发送正确的访问令牌,但我仍然收到禁止的响应:
<?php
namespace App;
使用Laravel \Socialite\Contracts\Factory as Socialite;
使用App \ Repositories\UserRepository;
使用Illuminate\Contracts\Auth\Guard;
class AuthenticateUser {
/ **
* @var UserRepository
* /
private $ users;
/ **
* @var Socialite
* /
私人$ 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 $ b $如果(!$ hasCode)返回$ this-> getAuthorizationFirst();返回值为$ b $ /
public function execute($ hasCode,AuthenticateUserListener $ listener)
{
$ var = $ this-> getGoogleUser();
$ user = $ this-> users-> findByUsernameOrCreate($ var);
\ Session :: put('token',$ var-> token);
\Auth :: login($ user,true);
return $ listener-> userHasLoggedIn($ user);
$ b public function logout()
{
\Auth :: logout();
return redirect('/');
$ b private function getAuthorizationFirst()
{
return \Socialize :: with('google' ) - >重定向();
$ b private function getGoogleUser()
{
return \Socialize :: with('google') - >用户();
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);
返回$ json;
$ b code
$ b这是我的AuthController.php p>
<?php namespace App \Http\Controllers;
使用App \AuthenticateUser;
使用App \AuthenticateUserListener;
使用App \Http\Requests;
使用App \Http\Controllers\Controller;
使用Laravel \Socialite\Two\GoogleProvider as Google;
使用Illuminate \Http\Request;
class AuthController extends Controller实现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();
$ b public function getContactList(AuthenticateUser $ authenticateUser)
{
$ response = $ authenticateUser-> getContactList();
dd($ response);
$ / code $ / pre
这是我的MainController.php <?php namespace App \Http\Controllers;
使用App \Http\Requests;
使用App \Http\Controllers\Controller;
使用Illuminate \Http\Request;
class MainController extends Controller {
public function index()
{
if(\Auth :: check())返回重定向('google_welcome');
返回重定向('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的社交网站,我真的很努力地利用我有限的知识,并且没有真正在网上找到很多文档,并且问题是我的雇主暗示我要么尽可能快地完成这项工作,要么他会告诉我出路......
解决方案我已经设法得到并最终通过了该错误,将我的getContactList函数更改为以下内容并添加适当的作用域:
public function getContactList()
{
$ config = [$ b $'client_id'=> env('CLIENT_ID',''),
'client-secret'=> env('CLIENT_SECRET',''),
];
$ client = new \GuzzleHttp\Client($ 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);
返回$ json;
I'm building a web app that has to interact with Google Contacts API and retrieve the Contact List of the authenticated user, but I'm getting
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
Here's my AuthenticateUser.php where I have included the getContactList function, I'm trying to make the Guzzle Request to the Google Server and managed to send the correct access token by storing it in a session variable, but I'm still getting a forbidden response :
<?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;
}
}
Here's my 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);
}
}
Here's my 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);
}
}
I'm fairly new to the PHP and Laravel universe, what's with trying to immediately use Google APIs and packages such as socialite which is using oAuth 2. I have really struggled to make the most out of my limited knowledge and haven't really found much documentation online, and the problem is my employer hinted that I either have to complete this as fastly as possible or he's gonna show me the way out...
解决方案 I have managed to get by and finally got past that error, changing my getContactList function to the following and adding the proper scope :
public function getContactList()
{
$config = [
'client_id' => env('CLIENT_ID', ''),
'client-secret' => env('CLIENT_SECRET', ''),
];
$client = new \GuzzleHttp\Client($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;
这篇关于在Laravel使用Guzzle和Socialite向Google API发出请求5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!