Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Google oauth getAccessToken()null_Php_Laravel_Laravel 5_Google Api - Fatal编程技术网

Php Google oauth getAccessToken()null

Php Google oauth getAccessToken()null,php,laravel,laravel-5,google-api,Php,Laravel,Laravel 5,Google Api,我想将GoogleOAuth添加到我的web应用程序中,我已经在使用php客户端库阅读关于GoogleOAuth的google文档,但我遇到了问题 这是我的密码 public function googleOauth() { $client = new Google_Client(); $client->setAuthConfig(base_path('google_client.json')); //$client->setAcces

我想将GoogleOAuth添加到我的web应用程序中,我已经在使用php客户端库阅读关于GoogleOAuth的google文档,但我遇到了问题

这是我的密码

public function googleOauth() {
        $client = new Google_Client();
        $client->setAuthConfig(base_path('google_client.json'));
        //$client->setAccessType("offline");
        $client->setIncludeGrantedScopes(true); 
        $client->addScope(Google_Service_Blogger::BLOGGER);
        $client->setRedirectUri(route('googleOauthCallback'));

        return redirect($client->createAuthUrl());
    }

    public function googleOauthCallback(Request $params) {
       if ($params->code) {
          $client = new Google_Client();
          $client->authenticate($params->code);

          dd($client->getAccessToken());
       }
但是
$client->getAccessToken()
返回
null


如何修复它?

我认为问题在于你没有向谷歌提出认证和取回令牌的请求。你应该做:

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = url('/');
$client->setRedirectUri($redirect);

//redirect to google server to get the token 
return Redirect::to( $client->createAuthUrl() );
如果身份验证成功,google会将您重定向到您使用
$client->setRedirectUri($redirect)
设置的页面

在该页面中,您可以:

//authenticate using the parameter $_GET['code'] you got from google server
$client->authenticate( $request->input('code') );

//get the access token
$tokens = $client->getAccessToken();

尝试转储
authenticate()
方法调用的返回值。基于,您应该会看到更多信息。数组:2[▼ “错误”=>“无效的\u请求”“错误描述”=>“无法根据请求确定客户端ID。”]谢谢,已解决。我只是将方法googleOauth()上的代码重复到googleOauthCallback()上