Gmail API签名服务器端

时间:2016-11-03 19:44:01

标签: php gmail gmail-api signature

我想更改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 for Work。

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

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

我按照指示更改了范围,现在我的范围是:

$客户端 - &GT; setScopes([&#39; https://www.googleapis.com/auth/gmail.settings.basic&#39;,&#39; https://www.googleapis.com/auth/gmail.settings.sharing&#39;]);

我还在Google上添加了permision(/ AdminHome?chromeless = 1#OGX:ManageOauthClients)到我的服务帐户密钥。

我尝试过API资源管理器,但它确实有用。当我更改范围时,错误更改为:

{&#34;错误&#34;:{&#34;错误&#34;:[{&#34;域名&#34;:&#34;全球&#34;,&#34;原因&#34 ;:&#34; failedPrecondition&#34;,&#34; message&#34;:&#34; Bad Request&#34; }],&#34;代码&#34;:400,&#34;消息&#34;:&#34;错误请求&#34; }}

我正在使用此命令:

的var_dump($ gmail-&GT; users_settings_sendAs-&GT;更新(&#34; someEmail@myDomain.com.br",&#34; someEmail@myDomain.com.br",$签名)) ;

我也尝试了

的var_dump($ gmail-&GT; users_settings_sendAs-&GT;获得(&#34; someEmail@myDomain.com.br",&#34; someEmail@myDomain.com.br"));

但我收到同样的错误。

3 个答案:

答案 0 :(得分:0)

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

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

希望这能解决您的问题

答案 1 :(得分:0)

当您没有请求访问您尝试使用的API所需的正确或完整范围时,将返回错误 403或权限不足。以下是您可以使用Gmail API的list of scopes with description

要了解您使用的范围,您可以使用以下方法进行验证:

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

  

注意:您需要包含您正在使用和制作的所有范围   确保启用在Developer Console中使用的所有API。   这Delegating domain-wide authority to the service account   也可能有帮助。

有关更多信息,请查看以下相关的SO问题:

答案 2 :(得分:0)

谢谢大家。

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

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创建了它。

注意力 - &gt;您需要授予Gmail权限,并创建服务器密钥(具有域范围访问权限)。