我正在尝试使用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窗体-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>';
?>
=&GT; 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 :(得分: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);