Google API - 从Oauth2请求令牌返回空令牌 [英] Google API - request for token from Oauth2 returns null token

查看:190
本文介绍了Google API - 从Oauth2请求令牌返回空令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于凭据,我在 https://console.developers.google.com 上创建了一个开发者帐户。 >,我创建了一个项目,然后我从API Manager创建了凭据。我使用google / apiclient:1.1。*包。我认为这是凭证的问题。

For credentials, I have created an developer account on https://console.developers.google.com, I have created a project and then i have created credentials from API Manager. I use "google/apiclient": "1.1.*" package. I think it is a problem with credentials.

    $OAUTH2_CLIENT_ID = 'XXXXX-rvm1l9b1nvht9je1ic0bbe05ab5gvhbg.apps.googleusercontent.com';
    $OAUTH2_CLIENT_SECRET = 'XXXXXXP90L_DLD3Nrc_rT4zGD';

    $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);


    $token = $client->getAccessToken();
    dd($token);


推荐答案

我认为问题在于您没有请求Google验证并取回令牌。你应该这样做:

I think the problem is you're not making the request to Google to authenticate and get back the token. You should do:

$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会将您重定向到您使用 $客户端 - > setRedirectUri($重定向)

If the authentication succeeds, google will redirect you to the page you set with $client->setRedirectUri($redirect).

在该页面中,您可以:

In that page you can:

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

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

这篇关于Google API - 从Oauth2请求令牌返回空令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆