3

我正在尝试使用 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 内容类型:application/x-www-form-urlencoded

code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFF&client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com&client_secret=hDBmMRhz7eJRsM9Z2q1oFBSe&redirect_uri=http://localhost/oauth2callback&grant_type=

=> 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);                                          
 ?>
4

1 回答 1

1

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

只需将参数作为键值对提供,如本示例中所做的那样,因此您不必关心 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);
于 2012-08-29T14:08:40.683 回答