Tyler Jones
Tyler Jones

Reputation: 424

How to turn on email forwarding with Google Script

A brief description of the project: I am looking to toggle the email forwarding option in the settings of one of my gmail accounts through a google script. This will be a function I would like to call every night between certain hours forwarding my mail from main_email@gmail to secondary_email@gmail.

I am having a difficult time finding the easiest way to toggle this through a google script. The simplest solution seems to be described here where they use an HTTP request. However in all honesty I don't completely understand how it all works, much less if it is the simplest way.

https://developers.google.com/gmail/api/v1/reference/users/settings/updateAutoForwarding

The code that I try and run on the gmail account to enable/disable email forwarding is the following:

function updateForwarding() {
    var userID = "[email protected]"
    var response = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/" + userID + "/settings/autoForwarding", {
        method: 'put',
        enabled: true,
        emailAddress: "[email protected]",
        disposition: "leaveInInbox"
    });

    Logger.log(response.getContentText());

}

However I get the following error:

Request failed for https://www.googleapis.com/gmail/v1/users/[email protected]/settings/autoForwarding returned code 401. Truncated server response: { "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", ... (use muteHttpExceptions option to examine full response) (line 4, file "Code")

I recognize this is shows I need to provide credentials for making the request, but I don't understand how I would do that. I read on the tutorial (https://developers.google.com/gmail/api/auth/about-auth) I need to authorize my app with gmail and get an API key, so I have gone to the google developers console to create that. However, I have no idea how to authenticate or make the call through a Google script after a few hours of google.

Here are the key and secret I was given: enter image description here

Is this the easiest solution to toggle gmail forwarding? If so, how do I authenticate my call? If not, what is the easiest solution to being able to toggle my gmail forwarding off/on?

Upvotes: 1

Views: 1602

Answers (2)

Ritesh Nair
Ritesh Nair

Reputation: 3355

You need to pass oAuth token in header information

   function updateForwarding() {
        var userID = "[email protected]";
        var header = {
         Authorization: 'Bearer ' + ScriptApp.getOAuthToken(),
         }
        var response = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/" + userID + "/settings/autoForwarding", {
            method: 'put',
            enabled: true,
            headers: header, 
            emailAddress: "[email protected]",
            disposition: "leaveInInbox"
        });

        Logger.log(response.getContentText());

    }

Upvotes: 1

John
John

Reputation: 339

As noted in the authorization section of https://developers.google.com/gmail/api/v1/reference/users/settings/updateAutoForwarding, you need to use OAuth with the given scopes to make that call, not just an API key. You appear to have a client id, but you need to plug this into a library to handle the OAuth process for you. The OAuth process will then give you a Bearer token to add to your request (although most OAuth libraries will handle this for you).

It looks like https://github.com/googlesamples/apps-script-oauth2 is the current recommened way to do this if you're using UrlFetchApp (based on https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app).

Upvotes: 0

Related Questions