1. <small id='lM1f7'></small><noframes id='lM1f7'>

        <tfoot id='lM1f7'></tfoot>

      2. <i id='lM1f7'><tr id='lM1f7'><dt id='lM1f7'><q id='lM1f7'><span id='lM1f7'><b id='lM1f7'><form id='lM1f7'><ins id='lM1f7'></ins><ul id='lM1f7'></ul><sub id='lM1f7'></sub></form><legend id='lM1f7'></legend><bdo id='lM1f7'><pre id='lM1f7'><center id='lM1f7'></center></pre></bdo></b><th id='lM1f7'></th></span></q></dt></tr></i><div id='lM1f7'><tfoot id='lM1f7'></tfoot><dl id='lM1f7'><fieldset id='lM1f7'></fieldset></dl></div>

        • <bdo id='lM1f7'></bdo><ul id='lM1f7'></ul>
        <legend id='lM1f7'><style id='lM1f7'><dir id='lM1f7'><q id='lM1f7'></q></dir></style></legend>

        使用服务帐户,getAccessToken() 返回 null

        时间:2023-05-31
          • <legend id='beIkz'><style id='beIkz'><dir id='beIkz'><q id='beIkz'></q></dir></style></legend>

            <small id='beIkz'></small><noframes id='beIkz'>

            <i id='beIkz'><tr id='beIkz'><dt id='beIkz'><q id='beIkz'><span id='beIkz'><b id='beIkz'><form id='beIkz'><ins id='beIkz'></ins><ul id='beIkz'></ul><sub id='beIkz'></sub></form><legend id='beIkz'></legend><bdo id='beIkz'><pre id='beIkz'><center id='beIkz'></center></pre></bdo></b><th id='beIkz'></th></span></q></dt></tr></i><div id='beIkz'><tfoot id='beIkz'></tfoot><dl id='beIkz'><fieldset id='beIkz'></fieldset></dl></div>
              <bdo id='beIkz'></bdo><ul id='beIkz'></ul>

              <tfoot id='beIkz'></tfoot>
                <tbody id='beIkz'></tbody>
                  本文介绍了使用服务帐户,getAccessToken() 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在运行以下 PHP 代码,使用此处找到的客户端库:https://code.google.com/p/google-api-php-client/.我没有收到任何此代码的错误,但是当我调用 getAccessToken() 时,它返回 null.

                  I am running the following PHP code, using the client libraries found here: https://code.google.com/p/google-api-php-client/. I do not get any errors for any of this code, but when I call getAccessToken(), it returns null.

                  我已允许在我的个人日历上访问此服务帐户,并已通过 API 控制台授予对项目的完全访问权限.

                  I have allowed access to this service account on my personal calendar, and have granted full access to the project via the API Console.

                  有什么想法吗?

                  require_once 'google-api-php-client/src/Google_Client.php';
                  
                  const CLIENT_ID = 'blahblahblah';
                  const SERVICE_ACCOUNT_NAME = 'blahblahblah@developer.gserviceaccount.com';
                  const KEY_FILE = 'path/to/privatekey.p12';
                  
                  $google_client = new Google_Client(); // created only to initialized static dependencies
                  $client = new Google_OAuth2(); // you really just need Google_OAuth2
                  
                  $key = file_get_contents(KEY_FILE);  
                  
                  $client->setAssertionCredentials(
                      new Google_AssertionCredentials(
                          SERVICE_ACCOUNT_NAME,
                          array('https://www.googleapis.com/auth/calendar'),
                          $key
                      )
                  );
                  
                  var_dump($client->getAccessToken());
                  

                  推荐答案

                  出于某种原因,这似乎有效:

                  For some reason, this seemed to work:

                  require_once 'google-api-php-client/src/Google_Client.php';
                  
                  const CLIENT_ID = 'blahblahblah';
                  const SERVICE_ACCOUNT_NAME = 'blahblahblah@developer.gserviceaccount.com';
                  const KEY_FILE = 'path/to/privatekey.p12';
                  const CALENDAR_SCOPE = "https://www.googleapis.com/auth/calendar";
                  
                  $key = file_get_contents(KEY_FILE);
                  $auth = new Google_AssertionCredentials(
                      SERVICE_ACCOUNT_NAME,
                      array(CALENDAR_SCOPE),
                      $key
                  );
                  
                  $client = new Google_Client();
                  $client->setScopes(array(CALENDAR_SCOPE));
                  $client->setAssertionCredentials($auth);
                  $client->getAuth()->refreshTokenWithAssertion();
                  $accessToken = $client->getAccessToken();
                  
                  $client->setClientId(CLIENT_ID);
                  

                  如果有人可以解释为什么这是有效的,请编辑此答案或评论!

                  If someone can explain why this worked, please edit this answer or comment!

                  这篇关于使用服务帐户,getAccessToken() 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 OAuth Refresh Token 获取新的 Access Token - Google API 下一篇:如何在 Google 日历中使用 OAuth 以仅访问一个日历?

                  相关文章

                  <legend id='O1HK3'><style id='O1HK3'><dir id='O1HK3'><q id='O1HK3'></q></dir></style></legend>
                    1. <i id='O1HK3'><tr id='O1HK3'><dt id='O1HK3'><q id='O1HK3'><span id='O1HK3'><b id='O1HK3'><form id='O1HK3'><ins id='O1HK3'></ins><ul id='O1HK3'></ul><sub id='O1HK3'></sub></form><legend id='O1HK3'></legend><bdo id='O1HK3'><pre id='O1HK3'><center id='O1HK3'></center></pre></bdo></b><th id='O1HK3'></th></span></q></dt></tr></i><div id='O1HK3'><tfoot id='O1HK3'></tfoot><dl id='O1HK3'><fieldset id='O1HK3'></fieldset></dl></div>

                      <small id='O1HK3'></small><noframes id='O1HK3'>

                      <tfoot id='O1HK3'></tfoot>

                        <bdo id='O1HK3'></bdo><ul id='O1HK3'></ul>