Google api 在PHP中获取Google YouTube API服务帐户访问令牌
我正试图从谷歌获得一个访问令牌,这样我就可以使用“服务帐户”将视频自动上传到YouTube 此代码:Google api 在PHP中获取Google YouTube API服务帐户访问令牌,google-api,youtube-api,service-accounts,Google Api,Youtube Api,Service Accounts,我正试图从谷歌获得一个访问令牌,这样我就可以使用“服务帐户”将视频自动上传到YouTube 此代码: $credentials = array( 'client_id' => $my_client_id ); $jwt = JWT::encode($credentials, $private_key); $client = new Google_Client(); if ($client->authenticate($jwt)) { // do s
$credentials = array(
'client_id' => $my_client_id
);
$jwt = JWT::encode($credentials, $private_key);
$client = new Google_Client();
if ($client->authenticate($jwt))
{
// do something
}
失败,但出现此异常:
Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_request: Client must specify either client_id or client_assertion, not both'' in /home/google/client/google-api-php-client/src/Google/Auth/OAuth2.php:120
我哪里做错了
泰 我不是专家,但您可能应该考虑删除您行末尾的“,”:
'client_id' => $private_key['client_id'],
//'client_email' => $private_ket['client_email']
改为:
'client_id' => $private_key['client_id']
//'client_email' => $private_ket['client_email']
我使用了这个例子来让oauth工作。这可能会有帮助:
您也可以在此处尝试oauth测试:
祝你好运 我遗漏了一大部分文档,如下所示: 我还忽略了算法必须是RSA256,而不是JWT PHP encode函数中默认的HSA256 此外,我还需要直接发布一个请求,以获得对端点的访问令牌: 服务帐户的Google private JSON私钥对于openssl的使用也是无效的,因为最后一个字符被编码/包含为: \u003d 从字面上讲,将其替换为: = 解决了这个问题 这是我现在正在工作的代码(ish,见结语): 不幸的是,出于某种原因,我得到的答复是: {“error”:“unauthorized_client”,“error_description”:“请求中的unauthorized client或scope.” 怀疑我的服务帐户还没有上传到YouTube的权利
$claimset = array(
'iss' => $client_email,
'scope' => 'https://www.googleapis.com/auth/youtube.upload',
'aud' => 'https://www.googleapis.com/oauth2/v3/token',
'exp' => time() + 1800,
'iat' => time(),
'sub' => 'my google account email@gmail.com'); // not sure if reqd
$jwt = JWT::encode($claimset, $private_key, 'RS256');
// Now need to get a token by posting the above to:
// https://www.googleapis.com/oauth2/v3/token
# Our new data
$data = array(
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' => $jwt
);
# Create a connection
$url = 'https://www.googleapis.com/oauth2/v3/token';
$ch = curl_init($url);
# Form data string
$postString = http_build_query($data, '', '&');
# Setting our options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# Get the response
$response = curl_exec($ch);
curl_close($ch);
print "and here is what we got: ";
print_r($response);
exit;