Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insufficient Permission [403] error while modifying the Message Label using gmail API in c#

Tags:

c#

gmail-api

I am trying to read the gmail mail message, using gmail api, and after reading the mail, I am removing the message label, so that I don't need to process it again. I am able to read the mail successfully, but when I am trying to modify the message Label
(service.Users.Messages.Modify(mods, userId, messageId).Execute();)

then I am getting the error message:

An error occurred: Google.Apis.Requests.RequestError
Insufficient Permission [403]
Errors [
Message[Insufficient Permission] Location[ - ] Reason[insufficientPermis
sions] Domain[global]>
].

I am not able to figure out, what may have gone wrong? Thanks in advance.

like image 646
Sujit Kumar Avatar asked Jul 28 '15 06:07

Sujit Kumar


3 Answers

you need to add the priviliges to the scope variable and then to delete the file storedCredentials (C:\Users\Administrateur.credentials.. )

like image 108
mohamed karim sfayhi Avatar answered Oct 12 '22 23:10

mohamed karim sfayhi


I had similar problems with a console application using a Service Account API key. After adding all the necessary Scopes, as mentioned by Tholle above, the application has to be updated with the necessary permissions in the Google admin console. To do this, make sure you go to Admin console and remove the current app and execute the program again to get a new token with new permissions with the updated scope.

Another way to accomplish the same is to do what Mohamed has mentioned above. That is to remove the JSON file from the "...User\[UserName]\.credetials\[apiCredentialName].json" folder. This will force the app to authenticate and get a new token. Hope this helps some one :-)

like image 42
Elango M Avatar answered Oct 13 '22 00:10

Elango M


I solved this problem by changing the Scopes variable. I assume you started with the QuickStart template which allows ReadOnly access only. You can change the scope as below:

using Google.Apis.Gmail.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GmailQuickstart
{
    class Program
    {
        // CHANGE YOUR SCOPE HERE AND DELETE YOUR CREDENTIALS.JSON FILE IN THE PROJECT.
        static string[] Scopes = { GmailService.Scope.GmailReadonly, GmailService.Scope.GmailModify };
        static string ApplicationName = "Gmail API .NET Quickstart";

Reminder: After you changed the scope and delete your credentials.json, please re-authenticate your access from the gmail api again.

Hope it works out for you!

like image 20
Calvin Tey Avatar answered Oct 13 '22 01:10

Calvin Tey