Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Gmail API C# code

    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 2.86k
    Answer it

    hi ,

    im working on a function that will save the attachment file in Email via Gmail using gmail API code(C#).

    but i keep getting an error that saying : 

    An unhandled exception of type 'Google.Apis.Auth.OAuth2.Responses.TokenResponseException' occurred in Google.Apis.dll

    Additional information: Error:"unauthorized_client", Description:"", Uri:""

     

    here is the code :

    using Google.Apis.Auth.OAuth2;
    using Google.Apis.Gmail.v1;
    using Google.Apis.Gmail.v1.Data;
    using Google.Apis.Services;
    using Google.Apis.Util.Store;
    using AE.Net.Mail;
    
    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
        {
            // If modifying these scopes, delete your previously saved credentials
            // at ~/.credentials/gmail-dotnet-quickstart.json
            static string[] Scopes = { GmailService.Scope.GmailReadonly };
            static string ApplicationName = "Gmail API .NET Quickstart";
    
            static void Main(string[] args)
            {
    
                UserCredential credential;
    
                using (var stream =
                    new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
                {
                    string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                    credPath = Path.Combine(credPath, ".credentials/gmail-dotnet-quickstart.json");
    
                    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "Sapir", CancellationToken.None, new FileDataStore(credPath, true)).Result;
                    Console.WriteLine("Credential file saved to: " + credPath);
                }
    
                // Create Gmail API service.
                var service = new GmailService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = ApplicationName,
                });
    
                // Define parameters of request.
                UsersResource.LabelsResource.ListRequest request = service.Users.Labels.List("me");
    
             
                IList<Label> labels = request.Execute().Labels;
                Console.WriteLine("Labels:");
                if (labels != null && labels.Count > 0)
                {
                    foreach (var labelItem in labels)
                    {
                        Console.WriteLine("{0}", labelItem.Name);
                    }
                }
                else
                {
                    Console.WriteLine("No labels found.");
                }
                Console.Read();
    
            }
        }
    }

     

    can you please help me know what is the problem ?

    thank you

 2 Answer(s)

  • Here you need to understand is that while using these services you need to give permission to your account for allowing these kind of services this is the primary requirement.

    Secondly while doing anything using API you need to use the API key provided to access the services supported by it .

    These things needs to be done from your end.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: