muttley91
muttley91

Reputation: 12684

Log in to Gmail API

I'm trying to authenticate my web app with the Gmail API using my Client ID and Client Secret. The authentication in the "quick start" example, located here, seems oddly complicated. Step 4, however, states:

Note: The authorization flow in this example is greatly simplified for demonstration purposes and should not be used in web applications. For more information, see Authorizing your App with Gmail.

but the link provided there is broken. Has anyone successfully created a simple authentication for the Gmail API in C#?

I'm mostly asking this question so that it can serve as a resource for others who aren't able to find the answer to this.

Upvotes: 1

Views: 4949

Answers (1)

Arthur Thompson
Arthur Thompson

Reputation: 9225

The broken link should be: https://developers.google.com/gmail/api/auth/about-auth

For the C# Auth try something like:

// Create OAuth Credential.
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    new ClientSecrets
    {
        ClientId = "CLIENT_ID",
        ClientSecret = "CLIENT_SECRET"
    },
    new[] { GmailService.Scope.GmailModify },
    "user",
    CancellationToken.None).Result;

// Create the service.
var service = new GmailService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "Draft Sender",
});

ListDraftsResponse draftsResponse = service.Users.Drafts.List("me").Execute();

Upvotes: 3

Related Questions