首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PeopleAPI Error 403 Google bugs与否

PeopleAPI Error 403 Google bugs与否
EN

Stack Overflow用户
提问于 2018-05-17 15:33:24
回答 1查看 126关注 0票数 0

我按照PeopleAPI示例https://developers.google.com/people/v1/write-people尝试了CreateContact,但总是得到错误403身份验证范围不足。我已经将作用域设置为PeopleServiceService.Scope.Contacts

下面是我的完整代码

代码语言:javascript
复制
                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);                
        }

有什么需要帮忙的吗?

提亚

EN

回答 1

Stack Overflow用户

发布于 2021-06-08 04:08:36

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

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

代码语言:javascript
复制
    public static string[] scopes = { PeopleServiceService.Scope.Contacts };

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

运行InitializePeopleService,然后就可以使用PeopleService了。

代码语言:javascript
复制
    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;
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50385783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档