dongtong7990 2016-03-11 10:08
浏览 106
已采纳

Google API - 来自Oauth2的令牌请求返回null令牌

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);
  • 写回答

2条回答 默认 最新

  • doucan2102 2016-03-11 10:20
    关注

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

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog