Php YouTube oauth如何请求帐户详细信息
因此,通过使用youtube oauth文档,我想出了获取访问令牌的方法:Php YouTube oauth如何请求帐户详细信息,php,oauth,youtube-api,youtube-data-api,google-oauth,Php,Oauth,Youtube Api,Youtube Data Api,Google Oauth,因此,通过使用youtube oauth文档,我想出了获取访问令牌的方法: /** * Get api authorization * * @return string */ public function getAuthorizationUrl() { // Make redirect $this->params = [
/**
* 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);
}
$youtube的var\u dump
返回一个空数组:
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 oauth登录后获取用户id。您需要使用正确的访问令牌调用。在响应中,包括用户id。这是你的电话号码
这可能也有帮助
如果您询问如何获取当前已验证用户的YouTube用户名或YouTube用户ID,可以在对请求的响应中找到
你的意思是google plus idyes,抱歉,我的坏消息,账户信息如个人资料名称意味着你有访问令牌,使用访问令牌你想获得个人资料id和名称,是吗?这确实是我的意思,我已经保存了google plus帐户,但我不知道如何获取youtube频道id之类的详细信息。当我尝试请求时,它会返回一个空数组。我已经编辑了我的问题。