jon
jon

Reputation: 31

How to create draft in gmail api

I am trying to create a new draft for a user with the gmail api, however i am getting an error I dont understand.

I searched but can't find a way to fix this error in gmail api, i know how to fix it please help

error

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

my code

async function sendMessage() {
    const gmail = google.gmail({ version: 'v1', auth: oauth2Client });
    var drafts = await gmail.users.drafts.create({
        userId: "me",
        requestBody: {
            id: "",
            message: {
                raw: "LS0tLQpGcm9tOiBKb2huIERvZSA8c0BnbWFpbC5jb20+ClRvOiBNYXJ5IFNtaXRoIDxzQGdtYWlsLmNvbT4KU3ViamVjdDogU2F5aW5nIEhlbGxvCkRhdGU6IEZyaSwgMjEgTm92IDE5OTcgMDk6NTU6MDYgLTA2MDAKTWVzc2FnZS1JRDogPDEyMzRAbG9jYWwubWFjaGluZS5leGFtcGxlPgoKaGVsbG8KLS0tLQ=="
            }
        }
    }).catch((v) => {
        console.log(v)
    })
    
}

Upvotes: 3

Views: 637

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116908

message: 'Insufficient Permission', domain: 'global', reason: 'insufficientPermissions'

Means that when you authorized this user you did not request enough scopes of authorization in order to use this method.

The Quick start for node.js Has you use

const SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];

While users.drafts.create requires one of the following scopes

enter image description here

Which means you need to change the scope in your code reauthorize your application which will request the proper scope from your user.

tip

To remove authorization make sure to delete the file in token.json it contains the users access token and refresh token, once deleted it will request authorization of your application again

const TOKEN_PATH = path.join(process.cwd(), 'token.json');

Upvotes: 1

Related Questions