Gmail API 签名服务器端

Gmail API Signature Server Side

我想从我的 Gmail 域中更改所有 signatures。这个域有很多帐户,我需要从 server-side.

更改它

我正在使用 php,我的项目是这样开始的: php composer.phar 需要 google/apiclient:2.0

我写了一个代码,但是当我尝试更新一封电子邮件(如 teste@mydomain.com)时,我收到:

{ "error": { "errors": [ { "domain": "global", "reason": "insufficientPermissions", "message": "Insufficient Permission" } ], "code": 403, "message": "Insufficient Permission" } }

我的代码 (using API client library) 是这样的:

<?php

// initialize gmail
function getService() {
    try {
        include_once __DIR__ . '/vendor/autoload.php';

        $client = new Google_Client();

        $credentials_file = __DIR__ . '/credentials/gmailAPI.json';

        // set the location manually. Credential server-side
        $client->setAuthConfig($credentials_file);

        $client->setApplicationName("GmailAPI");
        $client->setScopes(['https://apps-apis.google.com/a/feeds/emailsettings/2.0/']);

        $gmail = new Google_Service_Gmail($client);

        return $gmail;
    } catch (Exception $e) {
        throw new Exception($e->getMessage());
    }
}

function updateSignature(&$gmail) {
    try {
        // Start sendAs
        $signature = new Google_Service_Gmail_SendAs();
        // Configure Signature
        $signature->setSignature("Any HTML text here.");

        // Update account and print answer
        var_dump($gmail->users_settings_sendAs->update("someEmail@myDomain.com.br","someEmail@myDomain.com.br",$signature));
    } catch (Exception $e) {
        throw new Exception($e->getMessage());
    }

}

try {
    $gmail = getService();
    updateSignature($gmail);
} catch (Exception $e) {
    echo $e->getMessage();
}

?>

我的凭据文件 (gmailAPI.json) 是一个服务帐户密钥,我正在使用 Google 工作。

我使用此域中的一个管理员帐户创建了此凭据。

My credential file is:

{
  "type": "service_account",
  "project_id": "myProjectId",
  "private_key_id": "myPrivateKeyid",
  "private_key": "myPrivateKey",
  "client_email": "gmailapi@projectid.iam.gserviceaccount.com",
  "client_id": "myId",
  "auth_uri": "url",
  "token_uri": "url",
  "auth_provider_x509_cert_url": "url",
  "client_x509_cert_url": "url"
}

编辑 1

我按照说明更改了示波器,现在我的示波器是:

$client->setScopes(['https://www.googleapis.com/auth/gmail.settings.basic','https://www.googleapis.com/auth/gmail.settings.sharing']);

我还向我的服务帐户密钥添加了对 Google (/AdminHome?chromeless=1#OGX:ManageOauthClients) 的权限。

我试过 API 资源管理器,它有效。当我更改范围时,错误更改为:

{ "error": { "errors": [ { "domain": "global", "reason": "failedPrecondition", "message" : "Bad Request" } ], "code": 400, "message": "Bad Request" } }

我正在使用这个命令:

var_dump($gmail->users_settings_sendAs->更新("someEmail@myDomain.com.br","someEmail@myDomain.com.br",$signature));

我也试过了

var_dump($gmail->users_settings_sendAs->get("someEmail@myDomain.com.br","someEmail@myDomain.com.br"));

但是我收到了同样的错误。

我认为您的访问令牌不包含您想要的所有 scopes。首先在 API 资源管理器中尝试。检查您的 scopes API 资源管理器请求是什么,然后将这些范围添加到您的代码(您获得许可的地方)。

https://developers.google.com/apis-explorer

希望这能解决您的问题

好吧,当您没有请求正确或完整的范围时返回错误 403 或权限不足,您需要访问您正在尝试访问的 API采用。这是您可以与 Gmail API.

一起使用的 list of scopes with description

要知道你正在使用的范围,你可以用这个来验证它:

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=xxxxxx

Note: You need to include all the scope that you are using and make sure you enable all the API that you use in the Developer Console. This Delegating domain-wide authority to the service account might also help.

有关详细信息,请查看这些相关的 SO 问题:

谢谢大家。

我尝试了很多代码,终于找到了一个有效的代码。

include_once __DIR__ . '/vendor/autoload.php';
// credential file (service account)
$credentials_file = __DIR__ . '/credentials/gmailAPI.json';
putenv('GOOGLE_APPLICATION_CREDENTIALS='.$credentials_file);
// Initialize Google Client
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
// scopes to change signature
$client->setScopes(['https://www.googleapis.com/auth/gmail.settings.basic','https://www.googleapis.com/auth/gmail.settings.sharing']);
// *important* -> Probably because delegated domain-wide access.
$client->setSubject("admEmailHere@test.com");
// Initialize Gmail
$gmail = new Google_Service_Gmail($client);
// set signature
$signature = new Google_Service_Gmail_SendAs();
$signature->setSignature("HTML code here.");
// update signature
$response = $gmail->users_settings_sendAs->update("admEmailHere@test.com","admEmailHere@test.com",$signature)->setSignature();
// get signature
$response = $gmail->users_settings_sendAs->get("admEmailHere@test.com","admEmailHere@test.com")->getSignature();
echo json_encode($response);

我注释了所有代码,我使用 https://developers.google.com/api-client-library/php/auth/service-accounts 创建它。

注意 -> 您需要授予 Gmail 权限,并创建服务器密钥(具有全域访问权限)。