首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Google Auth server to server

Google Auth server to server
EN

Stack Overflow用户
提问于 2022-11-29 16:18:11
回答 1查看 43关注 0票数 1

我需要一个服务,以操纵一个gmail帐户内的一个项目开发的PHP Symfony.

我发现了这个例子:https://github.com/googleapis/google-api-php-client/blob/main/docs/oauth-server.md,但比帮助更令人困惑.

我写了这段代码:

src/Service/Gmail.php

代码语言:javascript
运行
复制
<?php

namespace App\Service;

use Google\Client;


class Gmail
{
    private \Google\Service\Gmail $api;


    private function getGoogleClient(): Client
    {
        $credentialsPath = getenv('GOOGLE_APPLICATION_CREDENTIALS');
        if (empty($credentialsPath)) {
            throw new \Exception('You need to set env var GOOGLE_APPLICATION_CREDENTIALS');
        }

        if (!file_exists($credentialsPath)) {
            throw new \Exception('Credentials file path ' . getenv('GOOGLE_APPLICATION_CREDENTIALS') . ' set in GOOGLE_APPLICATION_CREDENTIALS does not exist');
        }

        $client = new Client();
        $client->useApplicationDefaultCredentials();
        return $client;
    }

    private function getApi(): \Google\Service\Gmail
    {
        if (!isset($this->api)) {
            $this->api = new \Google\Service\Gmail($this->getGoogleClient());
        }

        return $this->api;
    }

    public function getUserMessages($userId): \Google\Service\Gmail\ListMessagesResponse
    {
        return $this->getApi()->users_messages->listUsersMessages($userId);
    }
}

然后,我遵循了Google中为开发人员描述的步骤:

创建一个新项目:https://developers.google.com/workspace/guides/create-project?hl=en

但此时我不知道我需要什么样的凭据:"OAuth客户端ID“还是”服务帐户“?如果我选择"Oauth客户端ID“,我想我必须使用服务器端url的应用程序类型"Web应用程序”?还是我选择了“服务帐户”?

通过服务帐户,我得到了以下错误:

代码语言:javascript
运行
复制
In REST.php line 134:
                                                                                                                                                                                        
  {                                                                                                                                                                                     
    "error": {                                                                                                                                                                          
      "code": 401,                                                                                                                                                                      
      "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.  
  google.com/identity/sign-in/web/devconsole-project.",                                                                                                                                 
      "errors": [                                                                                                                                                                       
        {                                                                                                                                                                               
          "message": "Login Required.",                                                                                                                                                 
          "domain": "global",                                                                                                                                                           
          "reason": "required",                                                                                                                                                         
          "location": "Authorization",                                                                                                                                                  
          "locationType": "header"                                                                                                                                                      
        }                                                                                                                                                                               
      ],                                                                                                                                                                                
      "status": "UNAUTHENTICATED",                                                                                                                                                      
      "details": [                                                                                                                                                                      
        {                                                                                                                                                                               
          "@type": "type.googleapis.com/google.rpc.ErrorInfo",                                                                                                                          
          "reason": "CREDENTIALS_MISSING",                                                                                                                                              
          "domain": "googleapis.com",                                                                                                                                                   
          "metadata": {                                                                                                                                                                 
            "service": "gmail.googleapis.com",                                                                                                                                          
            "method": "caribou.api.proto.MailboxService.ListMessages"                                                                                                                   
          }                                                                                                                                                                             
        }                                                                                                                                                                               
      ]                                                                                                                                                                                 
    }                                                                                                                                                                                   
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-29 16:58:37

您尚未预先授权您的服务帐户委托给您域中的用户。

代码语言:javascript
运行
复制
require_once('../../vendor/autoload.php');

// Some user within your workspace domain
$user_to_impersonate = "your@domain.com";

$sender = $user_to_impersonate;
$to = 'another@domain.com';
$subject = 'Hello';
$messageText = 'How are you doing?';

// The path to your service account credentials goes here.
putenv("GOOGLE_APPLICATION_CREDENTIALS=credentials.json");
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject($sender);
$client->setApplicationName("Quickstart");
$client->setScopes(["https://mail.google.com/"]);
$service = new Google_Service_Gmail($client);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74617092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档