【问题标题】:YouTube API v2.0 - OAuth 2.0 AuthorizationYouTube API v2.0 - OAuth 2.0 授权
【发布时间】:2012-08-29 13:08:43
【问题描述】:

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

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

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

==> 我需要发送的示例发布请求

POST /o/oauth2/token HTTP/1.1 主机:accounts.google.com 内容类型: 应用程序/x-www-form-urlencoded

代码=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFF& client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com& client_secret=hDBmMRhz7eJRsM9Z2q1oFBSe& redirect_uri=http://localhost/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>';
?> 

=> 获得_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);                                          
 ?>

【问题讨论】:

  • 授权码一段时间后过期,只能使用一次。您确定使用的是最近生成且未使用的吗?
  • 是的,它大概有 5 秒的历史。

标签: php api curl oauth youtube


【解决方案1】:

您的$body2 字符串可能存在一些编码问题,因为curl_setopt() 期望那里有一个urlencoded 字符串。

只需将参数作为键值对提供,就像在this example 中所做的那样,因此您不必关心对字符串进行 urlencoding:

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

【讨论】:

  • 我做了一个 $json_response 的 var_dump,我得到了这个错误。 string(33) "{ "error" : "invalid_request" }"
  • 请用当前代码更新您的问题,以便我们查看。
  • 对我来说似乎完全合法。不知道然后:/
  • 啊,当然 ;) 也监督了这一点。如果您认为我的回答是正确的,您可以将其标记为“已接受”。谢谢!
猜你喜欢
  • 2020-04-11
  • 2013-04-06
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 2013-10-05
  • 2014-07-20
  • 1970-01-01
相关资源
最近更新 更多