【发布时间】:2017-01-16 14:46:40
【问题描述】:
因此,通过使用 youtube oauth 文档,我想出了这种获取访问令牌的方法:
/**
* Get api authorization
*
* @return string
*/
public function getAuthorizationUrl()
{
// Make redirect
$this->params = [
'client_id' => '######',
'redirect_uri' => '######',
'scope' => 'https://www.googleapis.com/auth/youtube',
'response_type'=> 'code',
'access_type' => 'offline'
];
$redirect_url = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($this->params);
return $redirect_url;
}
/**
* Get API token and save account list to db
*
* @param $code
*
* @return \App\Models\DynamicDashboard\ThirdPartyAccounts
*/
public function getCallbackUrl($code)
{
// Grab the returned code and extract the access token.
$this->params = [
'code' => $code,
'client_id' => '#####',
'client_secret' => '######',
'redirect_uri' => '######',
'grant_type' => 'authorization_code'
];
// Get access token
$command = 'curl --data "' . http_build_query($this->params) . '" https://accounts.google.com/o/oauth2/token';
exec($command, $token);
$output = implode('', $token);
$token = json_decode($output);
// Do a request using the access token to get the list of accounts.
$command = 'curl -H "Authorization: Bearer ' . $token->access_token . '" https://www.googleapis.com/oauth2/v1/userinfo';
$result = $this->getRequest($command);
//Do a request using the access token to get youtube account id.
$command = 'curl -H "Authorization: Bearer ' . $token->access_token . '"http://gdata.youtube.com/feeds/api/users/default?v=2';
exec($command, $youtube);
var_dump($youtube); exit;
//Do a request using the access token to get channel id.
$command = 'curl -H "Authorization: Bearer ' . $token->access_token . '"https://www.googleapis.com/youtube/v3/channels?part=id&mine=true';
exec($command, $channel);
$outputChannel = implode('', $channel);
$channelId = json_decode($outputChannel);
}
var_dump of $youtube 返回一个空数组:
array {
}
所以现在我已经成功保存了 google 帐户,但是如何从该帐户获取 youtube 帐户 ID 或频道 ID?我试着这样做:
//Do a request using the access token to get youtube account id.
$command = 'curl -H "Authorization: Bearer ' . $token->access_token . '"http://gdata.youtube.com/feeds/api/users/default?v=2';
exec($command, $youtube);
var_dump($youtube); exit;
//Do a request using the access token to get channel id.
$command = 'curl -H "Authorization: Bearer ' . $token->access_token . '"https://www.googleapis.com/youtube/v3/channels?part=id&mine=true';
exec($command, $channel);
$outputChannel = implode('', $channel);
$channelId = json_decode($outputChannel);
但两个变量:$youtube 和 $channelId 返回一个空数组。谁能告诉我为什么?感谢您的帮助!
【问题讨论】:
-
你的意思是google plus id
-
是的,对不起,我的错误,以及像个人资料名称这样的帐户信息
-
表示你已经获得了访问令牌,并且通过使用访问令牌你想获得配置文件ID和名称,对吧?
-
确实是这个意思
标签: php oauth youtube-api youtube-data-api google-oauth