• <small id='Avn8u'></small><noframes id='Avn8u'>

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

        • <bdo id='Avn8u'></bdo><ul id='Avn8u'></ul>

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

        时间:2023-10-02
        <i id='33hpk'><tr id='33hpk'><dt id='33hpk'><q id='33hpk'><span id='33hpk'><b id='33hpk'><form id='33hpk'><ins id='33hpk'></ins><ul id='33hpk'></ul><sub id='33hpk'></sub></form><legend id='33hpk'></legend><bdo id='33hpk'><pre id='33hpk'><center id='33hpk'></center></pre></bdo></b><th id='33hpk'></th></span></q></dt></tr></i><div id='33hpk'><tfoot id='33hpk'></tfoot><dl id='33hpk'><fieldset id='33hpk'></fieldset></dl></div>

            <tfoot id='33hpk'></tfoot>

            1. <small id='33hpk'></small><noframes id='33hpk'>

                <tbody id='33hpk'></tbody>
              • <bdo id='33hpk'></bdo><ul id='33hpk'></ul>

                  <legend id='33hpk'><style id='33hpk'><dir id='33hpk'><q id='33hpk'></q></dir></style></legend>
                  本文介绍了使用服务帐户,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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                  相关文章

                  最新文章

                  <tfoot id='FrAao'></tfoot>
                1. <legend id='FrAao'><style id='FrAao'><dir id='FrAao'><q id='FrAao'></q></dir></style></legend>

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

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

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