【问题标题】:PeopleAPI Error 403 Google bugs or notPeopleAPI 错误 403 Google 错误与否
【发布时间】:2018-05-17 07:33:24
【问题描述】:

我按照 PeopleAPI 示例 https://developers.google.com/people/v1/write-people 尝试了 CreateContact,但总是收到错误 403 Insufficient Authentication Scopes。 我已经将范围设置为 PeopleServiceService.Scope.Contacts

下面是我的完整代码

                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);                
        }

有什么帮助吗?

TIA

【问题讨论】:

标签: c# api google-people-api


【解决方案1】:

这是我的人员服务初始化代码。

我的电话看起来像这样,但它有更多代码 - 不能全部发布:

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

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

运行 InitializePeopleService 然后你就可以使用 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;
        }
    }

【讨论】:

猜你喜欢
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多