如何使用.net核心修复Google API(People.Connections.List)的400状态错误?

时间:2019-03-26 21:27:57

标签: c# .net-core google-people-api

我正在尝试从Google联系人API中获取联系人,每次尝试使用People.Connections.List时都会收到400个状态代码错误。

这是我的代码:

static void Main(string[] args)
{
    PeopleService service = AuthenticateUser();
    // Works:
    Person connectionsResponse = service.People.Get("people/me?personFields=names,phoneNumbers").Execute();
    // Doesn't Work:
    ListConnectionsResponse connectionsResponses = service.People.Connections.List("people/me?personFields=names,addresses").Execute();

}

private static PeopleService AuthenticateUser()
{
    UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        new ClientSecrets
                        {
                            ClientId = CLIENT_ID,
                            ClientSecret = CLIENT_SECRET
                        },
                    new[] { "profile", "https://www.googleapis.com/auth/contacts.readonly", "https://www.googleapis.com/auth/contacts", "https://people.googleapis.com/v1", PeopleService.Scope.Contacts }, // scopes
                        "me",
                        CancellationToken.None).Result;

    PeopleService ps = new PeopleService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = PEOPLE_API_NAME,
        ApiKey = PEOPLE_API_KEY
    });
    return ps;
}

我对此行有疑问:

ListConnectionsResponse connectionsResponses = service.People.Connections.List("people/me?personFields=names,addresses").Execute();

我按照https://developers.google.com/people/api/rest/v1/people.connections/list上的说明进行了尝试,并尝试了以下查询:

people/me?personFields=names  
/people/me?personFields=names  
people/me/connections?personFields=names

我还试图更改“ personFields”的值并收到相同的错误。

我正在使用带有以下软件包的.NetCore 2.1控制台应用程序:

<PackageReference Include="Google.Apis" Version="1.38.0" />
<PackageReference Include="Google.Apis.People.v1" Version="1.25.0.830" />

在上面的链接中使用Google“尝试此API”时,它可以正常工作,并且我得到以下卷曲代码:

curl \
'https://people.googleapis.com/v1/people/me/connections?personFields=names' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--compressed

如您所见,我尝试在代码中使用相同的链接,但返回的错误相同。

这是完整的错误消息:

Unhandled Exception: Google.GoogleApiException: Google.Apis.Requests.RequestError
Invalid personFields mask path: "addresses/connections". Valid paths are documented at https://developers.google.com/people/api/rest/v1/people/get. [400]
Errors [
        Message[Invalid personFields mask path: "addresses/connections". Valid paths are documented at https://developers.google.com/people/api/rest/v1/people/get.] Location[ - ] Reason[badRequest] Domain[global]
]
   at Google.Apis.Requests.ClientServiceRequest`1.ParseResponse(HttpResponseMessage response) in Path:line 192
   at Google.Apis.Requests.ClientServiceRequest`1.Execute() in C:\Apiary\2019-01-31.11-23-48\Src\Support\Gooenter code heregle.Apis\Requests\ClientServiceRequest.cs:line 116
   at PeopleAPI.Auth.Main(String[] args) in Path:line 31

希望您能为我提供帮助。

谢谢:)

0 个答案:

没有答案