Understanding How to Run the Gmail API Using the Service Account Type OAuth Client

Asked 2 years ago, Updated 2 years ago, 108 views

I am trying to set up a forwarding email address for users in Google Apps tenants by referring to the page below, but the API fails to respond.
developers.google.com/gmail/api/v1/reference/users/settings/forwardingAddresses

I have tried the following, but could you tell me if there are any wrong or missing steps?Thank you for your cooperation.

1. Create OAuth Client

Accessed the API Manager in the Google Developer Console and created an OAuth 2.0 client of type サービスService Account Client 」.Check "Enable Google Apps Domain-Wide Delegation" for created clients.

2. Granting authority

"Google Apps Management Screen
  ->Security->Advanced Settings
  - From > Manage API Client Access,
 Grant https://mail.google.com/" permissions to the created client

3. Get Access Tokens

Access tokens are obtained using the PHP library published on the page below.
developers.google.com/api-client-library/php/start/installation

The PHP codes for obtaining access tokens are as follows:

<?php

require_once realpath(dirname(__FILE__).'/Google-api-php-client/src/Google/autoload.php');

$client_email='[email protected]';
$private_key=file_get_contents('MyProject.p12');

$scopes=array("https://mail.google.com/");

$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    $scopes,
    $private_key
);

$client=newGoogle_Client();

$client->setAssertionCredentials ($credentials);
if($client->getAuth()->isAccessTokenExpired()){
  $client->getAuth()->refreshTokenWithAssertion();
}

var_dump($client->getAccessToken());

?>

4. Run Gmail API

URL: https://www.googleapis.com/gmail/v1/users/ email address/settings/forwardingAddresses
 methods:GET
 headers:
  Authorization:Bearer Access Token

→ The response to this request is the following error response:

Response Status: 400
 Response Body:

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

gmail

2022-09-30 21:18

1 Answers

This is what I noticed when I saw it. - If you want to set the forwarding address, isn't the method POST?
- One more thing, isn't the endpoint (URL) set correctly?"The ""email address"" part below should be ""userId""

https://www.googleapis.com/gmail/v1/users/ email address/settings/forwardingAddresses

Reference: https://developers.google.com/gmail/api/v1/reference/users/settings/forwardingAddresses/create


2022-09-30 21:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.