Php 通过谷歌API发送电子邮件

Php 通过谷歌API发送电子邮件,php,google-api,google-oauth,gmail-api,google-api-php-client,Php,Google Api,Google Oauth,Gmail Api,Google Api Php Client,我正在尝试使用谷歌API发送电子邮件 发送电子邮件控制器看起来像 public function sendMessage() { $client = self::getClient(); $service = new Google_Service_Gmail($client); $mailer = $service->users_messages; $message = (new \Swift_Message

我正在尝试使用谷歌API发送电子邮件

发送电子邮件控制器看起来像

    public function sendMessage()
    {
        $client = self::getClient();
        $service = new Google_Service_Gmail($client);
        $mailer = $service->users_messages;

        $message = (new \Swift_Message('Here is my subject'))
            ->setFrom('myemailaddress@myserver.com')
            ->setTo(['receiver@someserver.com' => 'Test Name'])
            ->setContentType('text/html')
            ->setCharset('utf-8')
            ->setBody('<h4>Here is my body</h4>');

        $msg_base64 = (new \Swift_Mime_ContentEncoder_Base64ContentEncoder())
            ->encodeString($message->toString());

        $message = new Google_Service_Gmail_Message();
        $message->setRaw($msg_base64);
        $message = $mailer->send('me', $message);
        print_r($message);
    }
当我尝试运行此命令时,我收到错误:

{ "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" } } 
credentials.json

{"web":{
  "client_id":"REDACTED",
  "project_id":"project-44",
  "auth_uri":"https://accounts.google.com/o/oauth2/auth",
  "token_uri":"https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
  "client_secret":"redacted",
  "access_token":"redacted"}}

我的代码有什么问题?当我试图打印$client时,它会显示所需的数据。或者我应该如何登录以正确使用它而不被打断?我以前登录过(几个小时前)。

您似乎错过了获取和/或设置访问令牌或刷新令牌的步骤。在中,是这段代码:

    // Load previously authorized token from a file, if it exists.
    // The file token.json stores the user's access and refresh tokens, and is
    // created automatically when the authorization flow completes for the first
    // time.
    $tokenPath = 'token.json';
    if (file_exists($tokenPath)) {
        $accessToken = json_decode(file_get_contents($tokenPath), true);
        $client->setAccessToken($accessToken);
    }

    // If there is no previous token or it's expired.
    if ($client->isAccessTokenExpired()) {
        // Refresh the token if possible, else fetch a new one.
        if ($client->getRefreshToken()) {
            $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        } else {
            // Request authorization from the user.
            $authUrl = $client->createAuthUrl();
            printf("Open the following link in your browser:\n%s\n", $authUrl);
            print 'Enter verification code: ';
            $authCode = trim(fgets(STDIN));

            // Exchange authorization code for an access token.
            $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
            $client->setAccessToken($accessToken);

            // Check to see if there was an error.
            if (array_key_exists('error', $accessToken)) {
                throw new Exception(join(', ', $accessToken));
            }
        }
        // Save the token to a file.
        if (!file_exists(dirname($tokenPath))) {
            mkdir(dirname($tokenPath), 0700, true);
        }
        file_put_contents($tokenPath, json_encode($client->getAccessToken()));
    }
    return $client;
}
把它分解一下:

  • 您需要通过将用户发送到授权URL(
    $client->createAuthUrl()
    )来请求用户的授权
  • 然后将代码交换为访问令牌(
    $client->fetchAccessTokenWithAuthCode($authCode)
  • 然后在您的客户端上设置它(
    $client->setAccessToken($accessToken)
    ),或者保存它以备将来使用
  • 除了访问令牌之外,您还需要保存返回的刷新令牌,这样就不必每次重新登录(
    $client->fetchAccessTokenWithRefreshToken($refreshToken)
    )。如果您已经完成了此过程,那么您的访问令牌可能已过期,您需要使用刷新令牌或重新验证才能再次获得访问权限


    如果您开发此应用程序的用户不仅仅是您自己,您可能希望了解我在哪里工作。它们处理获取授权、刷新令牌等过程,同时加密令牌并为每个用户存储令牌。

    在哪里设置客户端ID和客户端机密?()它存储在credentials.json中,我应该在其他地方复制它吗?检查我的问题我已经用凭据更新了它file@RedEclipse不要把你的信用档案贴在任何你认为应该保密的地方。我一直把它们作为例子,但它们被大量修改,以不同的方式感谢你的回答。虽然我以前尝试过,但在STDIN方面出现了PHP错误。我想,如果intead每次都要求代码,那么手动将其设置为=$accessToken=$client->fetchAccessTokenWithAuthCode(“keytoken”)?它运行良好-没有错误,但仍然没有收到任何电子邮件。。。是不是因为我在本地主机上测试它,而不是在提供的域名上(在谷歌API中)?啊,它工作了(需要等待大约10分钟)!嗯,我希望钥匙代币在那之前不会改变。。
        // Load previously authorized token from a file, if it exists.
        // The file token.json stores the user's access and refresh tokens, and is
        // created automatically when the authorization flow completes for the first
        // time.
        $tokenPath = 'token.json';
        if (file_exists($tokenPath)) {
            $accessToken = json_decode(file_get_contents($tokenPath), true);
            $client->setAccessToken($accessToken);
        }
    
        // If there is no previous token or it's expired.
        if ($client->isAccessTokenExpired()) {
            // Refresh the token if possible, else fetch a new one.
            if ($client->getRefreshToken()) {
                $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
            } else {
                // Request authorization from the user.
                $authUrl = $client->createAuthUrl();
                printf("Open the following link in your browser:\n%s\n", $authUrl);
                print 'Enter verification code: ';
                $authCode = trim(fgets(STDIN));
    
                // Exchange authorization code for an access token.
                $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
                $client->setAccessToken($accessToken);
    
                // Check to see if there was an error.
                if (array_key_exists('error', $accessToken)) {
                    throw new Exception(join(', ', $accessToken));
                }
            }
            // Save the token to a file.
            if (!file_exists(dirname($tokenPath))) {
                mkdir(dirname($tokenPath), 0700, true);
            }
            file_put_contents($tokenPath, json_encode($client->getAccessToken()));
        }
        return $client;
    }