2012-08-29 1391 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