【问题标题】:Getting Google YouTube API Service Account Access Token in PHP在 PHP 中获取 Google YouTube API 服务帐户访问令牌
【发布时间】:2015-02-11 04:00:01
【问题描述】:

我正在尝试从 Google 获取访问令牌,以便使用“服务帐户”将视频自动上传到 YouTube。

这段代码:

$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

我哪里错了?

TY!

【问题讨论】:

    标签: google-api youtube-api service-accounts


    【解决方案1】:

    我不是专家,但您可能应该考虑在此处删除行尾的“,”:

    '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 正常工作。这可能会有所帮助: http://msdn.microsoft.com/en-us/library/dn632721.aspx

    您也可以在这里尝试 oauth 测试: https://developers.google.com/oauthplayground/

    祝你好运!

    【讨论】:

      【解决方案2】:

      我错过了如下所示的大部分文档:

      https://developers.google.com/accounts/docs/OAuth2ServiceAccount#creatingjwt

      我还错过了算法必须是 RSA256 而不是 JWT PHP 编码函数中默认的 HSA256。

      此外,我需要适当地发布一个请求,以直接获取对端点的访问令牌:

      https://www.googleapis.com/oauth2/v3/token

      服务帐户的 Google 私有 JSON 私钥也无法供 openssl 使用,因为最终字符被编码/包含为:

      \u003d

      从字面上看,将其替换为:

      =

      解决了这个问题。

      这是我现在工作的(ish,见结束语)代码:

      $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;
      

      不幸的是,由于某种原因,我得到的回复是:

      { "error": "unauthorized_client", "error_description": "未授权的客户端或请求范围。" }

      怀疑我的服务帐号还没有上传到 YouTube 的权限。

      【讨论】:

        猜你喜欢
        • 2012-11-25
        • 1970-01-01
        • 2015-04-29
        • 2021-12-02
        • 2019-01-05
        • 1970-01-01
        • 2017-10-07
        • 2016-04-29
        • 1970-01-01
        相关资源
        最近更新 更多