Gmail API returns 403 error code and “Delegation denied for

前端 未结 5 1853
礼貌的吻别
礼貌的吻别 2020-11-29 04:58

Gmail API fails for one domain when retrieving messages with this error:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 OK
{
  \"code         


        
相关标签:
5条回答
  • 2020-11-29 05:40

    I had the same issue before, the solution is super tricky, you need to impersonate the person you need to access gmail content first, then use userId='me' to run the query. It works for me.

    here is some sample code:

       users = # coming from directory service
       for user in users:
         credentials = service_account.Credentials.from_service_account_file(
            SERVICE_ACCOUNT_FILE, scopes=SCOPES)
         ####IMPORTANT######
         credentials_delegated = credentials.with_subject(user['primaryEmail'])
    
         gmail_service = build('gmail', 'v1', credentials=credentials_delegated)
    
         results = gmail_service.users().labels().list(userId='me').execute()
         labels = results.get('labels', [])
           for label in labels:
              print(label['name'])
    
    0 讨论(0)
  • 2020-11-29 05:47

    Recently I started exploring Gmail API and I am following the same approach as Guo mentioned. However, it is going to take of time and too many calls when we the number of users or more. After domain wide delegation my expectation was admin id will be able to access the delegated inboxes, but seems like we need to create service for each user.

    0 讨论(0)
  • 2020-11-29 06:03

    Seems like best thing to do is to just always have userId="me" in your requests. That tells the API to just use the authenticated user's mailbox--no need to rely on email addresses.

    0 讨论(0)
  • 2020-11-29 06:03

    Our users had migrated into a domain and their account had aliases attached to it. We needed to default the SendAs address to one of the imported aliases and want a way to automate it. The Gmail API looked like the solution, but our privileged user with roles to make changes to the accounts was not working - we kept seeing the "Delegation denied for " 403 error.

    Here is a PHP example of how we were able to list their SendAs settings.

    <?PHP
    
    //
    // Description:
    //   List the user's SendAs addresses.
    //
    // Documentation:
    //   https://developers.google.com/gmail/api/v1/reference/users/settings/sendAs
    //   https://developers.google.com/gmail/api/v1/reference/users/settings/sendAs/list
    //
    // Local Path:
    //   /path/to/api/vendor/google/apiclient-services/src/Google/Service/Gmail.php
    //   /path/to/api/vendor/google/apiclient-services/src/Google/Service/Gmail/Resource/UsersSettingsSendAs.php
    //
    // Version:
    //    Google_Client::LIBVER  == 2.1.1
    //
    
    require_once $API_PATH . '/path/to/google-api-php-client/vendor/autoload.php';
    
    date_default_timezone_set('America/Los_Angeles');
    
    // this is the service account json file used to make api calls within our domain
    $serviceAccount = '/path/to/service-account-with-domain-wide-delagation.json';
    putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $serviceAccount );
    
    $userKey = 'someuser@my.domain';
    
    // In the Admin Directory API, we may do things like create accounts with 
    // an account having roles to make changes. With the Gmail API, we cannot 
    // use those accounts to make changes. Instead, we impersonate
    // the user to manage their account.
    
    $impersonateUser = $userKey;
    
    // these are the scope(s) used.
    define('SCOPES', implode(' ', array( Google_Service_Gmail::GMAIL_SETTINGS_BASIC ) ) );
    
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();  // loads whats in that json service account file.
    $client->setScopes(SCOPES); // adds the scopes
    $client->setSubject($impersonateUser);  // account authorized to perform operation
    
    $gmailObj  = new Google_Service_Gmail($client);
    
    $res       = $gmailObj->users_settings_sendAs->listUsersSettingsSendAs($userKey);
    
    print_r($res);
    
    
    ?>
    
    0 讨论(0)
  • 2020-11-29 06:03

    I wanted to access the emails of fresh email id/account but what happened was, the recently created folder with '.credentials' containing a JSON was associated with the previous email id/account which I tried earlier. The access token and other parameters present in JSON are not associated with new email id/account. So, in order make it run you just have to delete the '.credentails' folder and run the program again. Now, the program opens the browser and asks you to give permissions.

    To delete the folder containing files in python

    import shutil
    shutil.rmtree("path of the folder to be deleted")
    

    you may add this at the end of the program

    0 讨论(0)
提交回复
热议问题