Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Bad Request [400] using Gmail API #22

Open
kiquenet opened this issue Feb 8, 2018 · 1 comment
Open

Error Bad Request [400] using Gmail API #22

kiquenet opened this issue Feb 8, 2018 · 1 comment

Comments

@kiquenet
Copy link

kiquenet commented Feb 8, 2018

I have this code, using Service Account:

`

            string[] Scopes = {
                Google.Apis.Gmail.v1.GmailService.Scope.MailGoogleCom,
                Google.Apis.Gmail.v1. GmailService.Scope.GmailModify,
                Google.Apis.Gmail.v1.GmailService.Scope.GmailCompose,
                Google.Apis.Gmail.v1.GmailService.Scope.GmailSend };

        var pathSecrets = @".\ContactosProject-dcbe8450d418.json";
        var serviceAccountEmail = "contactosservice@contactosproject.iam.gserviceaccount.com";

        var gmailSvc = GmailAPI.ServiceAccountHelper.AuthenticateServiceAccount(serviceAccountEmail, pathSecrets, Scopes);

        string plainText = "To: xxxxxxxxxxx@gmail.com\r\n" +
                           "Subject: subject Test\r\n" +
                           "Content-Type: text/html; charset=us-ascii\r\n\r\n" +
                           "<h1>Body Test </h1>";

        var newMsg = new Google.Apis.Gmail.v1.Data.Message();
        newMsg.Raw = Base64UrlEncode(plainText.ToString());
        gmailSvc.Users.Messages.Send(newMsg, "me").Execute();`

I use your AuthenticateServiceAccount class for samples.

I get the error:

Result StackTrace:
at Google.Apis.Requests.ClientServiceRequest1.<ParseResponse>d__34.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Google.Apis.Requests.ClientServiceRequest1.Execute()

Google.GoogleApiException: Google.Apis.Requests.RequestError
Bad Request [400]
Errors [
Message[Bad Request] Location[ - ] Reason[failedPrecondition] Domain[global]
]

The same error for

        Google.Apis.Gmail.v1.UsersResource.LabelsResource.ListRequest request = gmailSvc.Users.Labels.List("me");
        var Labels = request.Execute().Labels;

Any suggestions?

@kiquenet
Copy link
Author

Linda Lawton says:

Service accounts will only work with Gsuite email accounts. The admin of the Gsuite (domain) account will have to grant the service account access to the users email account. Unfortunately i don't have access to a Gsuite account so cant help you set it up. This might help Perform G Suite Domain-Wide Delegation of Authority

If this is a normal Gmail user account you are trying to access then you cant use a service account. You will either have to go though the SMTP / IMAP servers or using Oauth2 to authenticate the user.

Then, any full sample step-by-step using Oauth2 to authenticate the user?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant