doubiao9775 2016-11-03 19:44
浏览 102
已采纳

Gmail API签名服务器端

I want to change all signatures from my Gmail domain. This domain has many accounts, and I need to change it from server-side.

I'm using php, and I started my project with: php composer.phar require google/apiclient:2.0

I wrote one code, but when I try to update one email (like teste@mydomain.com), I receive:

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

My code (using API client library) is something like:

<?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();
}

?>

My credential file (gmailAPI.json) is one service account key, and I'm using Google for Work.

I created this credential using one administrator account from this domain.

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"
}

Edit 1

I changed the scopes as instructed, and now my scopes are:

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

I also added permision on Google (/AdminHome?chromeless=1#OGX:ManageOauthClients) to my service account key.

I tried API explorer and it works. When i changed the scopes, the error changed to:

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

I'm using this command:

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

I tried also

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

But I received same error.

  • 写回答

3条回答 默认 最新

  • doujingqu3030 2016-11-10 19:39
    关注

    Thank You everyone.

    I tried a lot of codes, and finally found one that works.

    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);
    

    I commented all code, and I used https://developers.google.com/api-client-library/php/auth/service-accounts to create it.

    Atention -> You need to give permission on Gmail, and create server key (with domain-wide access).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化