2012-08-29 1390 views
3

我正在尝试使用Oauth和yt API。我想尝试基于浏览器的YouTube上传。 但首先我必须让用户可以允许程序访问我的页面。 https://developers.google.com/youtube/2.0/developers_guide_protocol_browser_based_uploading#Browser_based_uploadingYouTube API v2.0 - OAuth 2.0授权

但首先我需要获得一个ACCESS_TOKEN,以便我需要一个OAuth 2.0访问令牌。但由于某种原因,我无法得到它。我做错了什么?

所以首先我去reddir.php并点击链接,以便重定向到允许程序使用我的yt帐户。

==>样品交请求我需要发送

POST/O /的oauth2 /令牌HTTP/1.1主机:accounts.google.com内容类型: 应用程序/ x-WWW-窗体-urlencoded

代码= 4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf & CLIENT_ID = 1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com & client_secret = hDBmMRhz7eJRsM9Z2q1oFBSe & REDIRECT_URI = HTTP://本地主机/ oauth2callback & grant_type = authorization_code

=> redir.php

<?php 
    $Access_token = 'https://accounts.google.com/o/oauth2/auth? 
         client_id=xxxxxxxxxxxx.apps.googleusercontent.com& 
         redirect_uri=http://sdesigns.be/UNSharp/obtain_token.php& 
         scope=https://gdata.youtube.com& 
         response_type=code& 
         access_type=offline';    

    echo '<a href="' . $Access_token . '">Click here to obtain a token2</a>'; 
?> 

=> obtain_token.php

<?php 
      //this file is 
    $client_id = 'xxxxxxxxxxxx.apps.googleusercontent.com'; 
    $client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxx'; 
    $redirect_uri = 'http://sdesigns.be/UNSharp/obtain_token.php'; 

    $author_code = $_GET["code"]; 

    echo 'Author code: <br>' .$author_code . '<br>'; 



    $oauth2token_url = "https://accounts.google.com/o/oauth2/token"; 
    $clienttoken_post = array(
          "code" => $code, 
          "client_id" => $client_id, 
          "client_secret" => $client_secret, 
          "redirect_uri" => $redirect_uri, 
          "grant_type" => "authorization_code" 
         ); 

    $curl = curl_init($oauth2token_url); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post); 
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 

    $json_response = curl_exec($curl); 

    echo 'RESULT code: <br>' ; 
    var_dump($json_response); 
    echo '<br>';  

    curl_close($curl);           
?> 
+0

授权码一段时间后到期,而且只能使用一旦。你确定你使用了最近生成和未使用的? – poplitea

+0

是的,它像5秒钟或其他东西。 – Sharpless

回答

1

您可能与您的$body2字符串有一些编码问题,如curl_setopt()正在等待一个urlencoded字符串。

只需提供参数键值对,如this example完成的,所以你不必关心URL编码字符串:

$oauth2token_url = "https://accounts.google.com/o/oauth2/token"; 
$clienttoken_post = array(
"code" => $code, 
"client_id" => $client_id, 
"client_secret" => $client_secret, 
"redirect_uri" => $redirect_uri, 
"grant_type" => "authorization_code" 
); 

$curl = curl_init($oauth2token_url); 

curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post); 
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 

$json_response = curl_exec($curl); 
curl_close($curl); 
+0

我做了$ json_response的var_dump,并且出现了这个错误。字符串(33)“{”error“:”invalid_request“}” – Sharpless

+0

请更新您的问题与当前的代码,所以我们可以看看它。 –

+0

我在问题中粘贴了我的代码。 – Sharpless