drt96856 2014-10-21 18:44
浏览 55
已采纳

YouTube OAuth 2.0 - GET请求从var_dump()获取NULL;

I've been working with YouTube OAuth 2.0 and i'm having some problems. I've been using the example and I've got up to the stage where the Google channel is being authenticated and redirecting back with a code.

When trying to send a GET request to grab some channel data from the OAuth scopes i'm getting a "NULL" response and i'm unsure why.

(Validate contains a working request)

echo "<a href='$validate'>Login with Google for advanced analytics</a>";


$channel_data = file_get_contents('https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DMINE&start-date=2014-08-01&end-date=2014-09-01&metrics=views&key={api_key}');
$channel_data = json_decode($channel_data, true);

echo "<br />";
var_dump($channel_data);

It seems to work in the example request found https://developers.google.com/youtube/analytics/v1/

  • 写回答

1条回答 默认 最新

  • doushou1298 2014-10-23 20:55
    关注

    This seems to be a misunderstanding of how to implement OAuth 2.0.

    This step-by-step instruction is a summary of the site I've linked:

    1. create a client id for web application in the developer console
    2. make your user go to the auth url
    3. when he/she completed the process, the user will be redirected to the redirect uri you specified.
    4. on this page, your php script has to grab the code-query ($_GET['code'])
    5. next, you have to make a request (containing the code) to a page which provides you with an access token and a refresh token. An access token expires after 1h, then you have to get a new one with your refresh token
    6. once you have the access token, you can start making api requests

    On the website linked at the top, you'll find detailed instructions. Hope this helps :)


    UPDATE

    In order to exchange the code for the tokens, you have to perform a post request. I don't think that'l work with file_get_contents. I recommend using curl here.

    This code should work for you, but it's not tested:

    $url = 'https://accounts.google.com/o/oauth2/token';
    
    $postfields = [
        'code'=>'YOUR_CODE',
        'client_id'=>'YOUR_CLIENT_ID',
        'client_secret'=>'YOUR_CLIENT_SECRET',
        'redirect_uri'=>'YOUR_REDIRECT_URI',
        'grant_type'=>'authorization_code'
    ];
    
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    
    curl_setopt($curl, CURLOPT_POST, count($postfields));
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postfields));
    
    $response = curl_exec($curl);
    curl_close($curl);
    
    $response = json_decode($response);
    

    $response now is an associative array with the keys access_token, token_type, expires_in, refresh_token. token_type is always Bearer and expires_in is always 3600. The other two are the important ones and you should store them safely!

    You can find your client id and client secret in the developer console.

    But please note that in this example, the ssl verification is disabled. That means your connection is not safe.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误