Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google api 谷歌服务对服务认证不起作用_Google Api_Google Api Php Client_Service Accounts_Google Calendar Api_Google Workspace - Fatal编程技术网

Google api 谷歌服务对服务认证不起作用

Google api 谷歌服务对服务认证不起作用,google-api,google-api-php-client,service-accounts,google-calendar-api,google-workspace,Google Api,Google Api Php Client,Service Accounts,Google Calendar Api,Google Workspace,我正试图让我的php应用程序使用服务到服务的身份验证来调用Google的日历API。我拥有对我的教育G套件的管理权限,并已执行以下步骤,如下所示: 创建了具有域权限的服务帐户,并保存了正确的凭据 使用我的ClientID(由UI确认)将日历(读写)范围添加到“托管API客户端访问”下的我的管理员帐户中 根据示例,对php API客户端进行了适当的调用,试图同时模拟管理员和另一个用户帐户 在这里: 但是,当我试图获取任何日历信息时,我仍然会遇到以下错误: Google_服务_异常:{“错误”:“未

我正试图让我的php应用程序使用服务到服务的身份验证来调用Google的日历API。我拥有对我的教育G套件的管理权限,并已执行以下步骤,如下所示:

  • 创建了具有域权限的服务帐户,并保存了正确的凭据
  • 使用我的ClientID(由UI确认)将日历(读写)范围添加到“托管API客户端访问”下的我的管理员帐户中
  • 根据示例,对php API客户端进行了适当的调用,试图同时模拟管理员和另一个用户帐户 在这里:
  • 但是,当我试图获取任何日历信息时,我仍然会遇到以下错误:

    Google_服务_异常:{“错误”:“未经授权的_客户端”,“错误描述”:“客户端未经授权使用此方法检索访问令牌。”}

    根据谷歌的说法,这通常意味着我没有授予整个域名的权限,但我肯定有。我尝试过其他谷歌API,结果也一样

    这是我的密码。谢谢你的任何想法或帮助

    <?php require_once __DIR__ . '/vendor/autoload.php';
    putenv('GOOGLE_APPLICATION_CREDENTIALS=****/client_secret.json');
    
    $user_to_impersonate = 'user@****.com';
    $user_scopes = array(
        Google_Service_Calendar::CALENDAR
    );
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    $client->setSubject($user_to_impersonate);
    $client->setScopes($user_scopes);
    $client->setAccessType('offline');
    
    $calendar = new Google_Service_Calendar($client);
    $calendarId = 'primary';
    $optParams = array(
        'maxResults' => 10,
        'orderBy' => 'startTime',
        'singleEvents' => TRUE,
        'timeMin' => date('c'),
    );
    $results = $calendar->events->listEvents($calendarId, $optParams);
    ...
    
    
    ?>
    
    
    
    客户端无权使用此方法检索访问令牌

    当您在Google开发者控制台上创建项目时,您应该已经创建了服务帐户凭据。然后,您将被提升为下载一个文件。该文件包含服务帐户的凭据

    用于连接到服务帐户的代码与用于使用浏览器或本机客户端凭据进行连接的代码不同

    servicecomport.php


    您似乎使用了正确的服务帐户代码。但是,您可能下载了错误的凭据文件,我建议您再次从Google开发者控制台下载该文件。

    在上面的步骤2中添加了不正确的作用域。希望这仍然能让一些人看到正确的步骤。

    我也遇到过这个问题,但只有在使用$client->setSubject(模拟电子邮件)时才会遇到这个问题;
    试着注释这一行,看看它给出了什么响应代码。

    您是否以某种方式解决了这个问题?我们遇到了完全相同的问题。是的,请参见下文。只是在第二步有一个输入错误。基本上,如果你正确地遵循所有步骤,你应该是ok的。如果您仍然有问题,并且有人可能会提供帮助,请随时一步一步地粘贴您的内容。
    require_once __DIR__ . '/vendor/autoload.php';
    // Use the developers console and download your service account
    // credentials in JSON format. Place the file in this directory or
    // change the key file location if necessary.
    putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/service-account.json');
    /**
     * Gets the Google client refreshing auth if needed.
     * Documentation: https://developers.google.com/identity/protocols/OAuth2ServiceAccount
     * Initializes a client object.
     * @return A google client object.
     */
    function getGoogleClient() {
        return getServiceAccountClient();
    }
    /**
     * Builds the Google client object.
     * Documentation: https://developers.google.com/api-client-library/php/auth/service-accounts
     * Scopes will need to be changed depending upon the API's being accessed. 
     * array(Google_Service_Analytics::ANALYTICS_READONLY, Google_Service_Analytics::ANALYTICS)
     * List of Google Scopes: https://developers.google.com/identity/protocols/googlescopes
     * @return A google client object.
     */
    function getServiceAccountClient() {
        try {   
            // Create and configure a new client object.        
            $client = new Google_Client();
            $client->useApplicationDefaultCredentials();
            $client->addScope([YOUR SCOPES HERE]);
            return $client;
        } catch (Exception $e) {
            print "An error occurred: " . $e->getMessage();
        }
    }