0

I tried CreateContact following PeopleAPI exmples https://developers.google.com/people/v1/write-people but always got error 403 Insufficient Authentication Scopes. I already set the scope to PeopleServiceService.Scope.Contacts

Below is my full code

                string[] Scopes = new string[] { PeopleServiceService.Scope.Contacts }; 

            UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = "xxxxxxx.apps.googleusercontent.com",
                ClientSecret = "xxxxx"
            },
            Scopes,
            "me",
            System.Threading.CancellationToken.None).Result;

         var peopleService = new Google.Apis.PeopleService.v1.PeopleServiceService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Test1"
        });

        try
        {
            //Create New COntact
            Person contactToCreate = new Person();

            List<Name> names = new List<Name>();
            names.Add(new Name() { GivenName = "a1test1", FamilyName = "zzz" });
            contactToCreate.Names = names;

            Google.Apis.PeopleService.v1.PeopleResource.CreateContactRequest request =
             new Google.Apis.PeopleService.v1.PeopleResource.CreateContactRequest(peopleService, contactToCreate);
            Person createdContact = request.Execute();

        }
        catch (Exception merr)
        {
            MessageBox.Show(merr.Message);                
        }

Any help please ?

TIA

1 Answers1

-1

Here is my people service init code.

My call looks like this, but it has lot more code - can't post it all:

    public static string[] scopes = { PeopleServiceService.Scope.Contacts };

    GoogleUtils.People people = new People();
    people.InitializePeopleService(scopes, userEmail);

Run InitializePeopleService then you are good to use PeopleService.

    using Google.Apis.PeopleService.v1;
    //using Google.Apis.PeopleService.v1.Data;

    public static string[] scopes = { PeopleServiceService.Scope.Contacts };
    private static PeopleServiceService peopleService;

    public void InitializePeopleService(string[] scopes, string impersonateAs, string clientSecretFilePath = "client_secret_svc.json")
    {
        PeopleService = new PeopleServiceService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = Auth.GetServiceAccountAuthorization(scopes: scopes, clientSecretFilePath: clientSecretFilePath, impersonateAs: impersonateAs)
        });

        if (PeopleService.Name != null)
            CurrentPersonEmail = impersonateAs;
        else
        {
            throw new Exception($"Failed to impersonate as {impersonateAs}");
        }
    }

    public static PeopleServiceService PeopleService
    {
        get
        {
            if (peopleService == null)
            {
                throw new Exception("Please initialize the service by calling InitializePeopleService.");
            }

            return peopleService;

        }
        set
        {
            peopleService = value;
        }
    }
Zunair
  • 1,085
  • 1
  • 13
  • 21
  • oh here is my auth code https://stackoverflow.com/questions/54868344/batch-request-sendas-emails – Zunair Jun 07 '21 at 20:11