Google Ruby 中的 PeopleApi - CreateContact 方法参数无效

Google PeopleApi in Ruby - CreateContact method invalid argument

我正在使用 'google-api-client' gem 从我的 Ruby 应用程序连接到 Google 人 API。 https://github.com/googleapis/google-api-ruby-client

我已经设法使其他方法起作用(list_person_connections、get_people、delete_person_contact 和 update_person_contact),但我无法获得 createContact(create_person_contact) google 人 API 工作的方法。

发送 post 后,出现此错误:

400 Caught error badRequest: Request contains an invalid argument.

这是创建仅包含名称字段的联系人的示例代码(我实际上还想在正文中发送电子邮件和电话号码参数,但它也 returns 同样的错误,所以我'我在这里给你最简单的例子):

require 'google/apis/content_v2'
require "google/apis/people_v1"
require "googleauth"
require "googleauth/stores/file_token_store"
require "fileutils"
require 'google/apis/people_v1'
require 'google/api_client/client_secrets'

client_secrets = Google::APIClient::ClientSecrets.load 'credentials.json'
auth_client = client_secrets.to_authorization
auth_client.update!(
    :scope => ['https://www.googleapis.com/auth/contacts', 'https://www.googleapis.com/auth/contacts.other.readonly'],
    :redirect_uri => 'http://localhost:3102/oauth2callback',
    :client_id => 'MY CLIENT ID',
    :client_secret => 'MY CLIENT SECRET',
    #:authorization_uri => 'https://accounts.google.com/o/oauth2/auth',
    :additional_parameters => {"access_type" => "offline", "include_granted_scopes" => "true"})
auth_uri = auth_client.authorization_uri.to_s

auth_client.code = 'THE AUTH CODE'
auth_client.fetch_access_token!
people = Google::Apis::PeopleV1::PeopleServiceService.new
people.authorization = auth_client

body = {:names => [{:givenName => "TEST"}]}
people.create_person_contact(body, person_fields: 'names')

问题就出在最后两行。被调用时,他们将其与正文一起发送:

Sending HTTP post https://people.googleapis.com/v1/people:createContact?personFields=names

它returns上面的错误,不管我怎么改。 在文档中,您实际上可以尝试并测试这个完全相同的代码并且它有效。

https://developers.google.com/people/api/rest/v1/people/createContact?authuser=2&apix=true&apix_params=%7B%22personFields%22%3A%22names%22%2C%22resource%22%3A%7B%22names%22%3A%5B%7B%22givenName%22%3A%22TEST%22%7D%5D%7D%7D

您可以像我一样填写请求正文表单,然后点击 EXECUTE 它将给出 200 OK 响应。

我看不出无效参数是什么。这也正是我在 update_person_contact 方法上所做的,并且该方法有效。

我已经在互联网上搜索过,但找不到有类似问题的人,文档也没有说明太多:https://www.rubydoc.info/github/google/google-api-ruby-client/Google%2FApis%2FPeopleV1%2FPeopleServiceService:create_person_contact

任何人有任何想法可以帮助我吗? 谢谢。

在几乎要放弃之后,我决定尝试一下 CURL 选项。所以我从上面链接的 'Try this API' 页面复制了它并将其翻译成 Ruby 代码。

curl --request POST \
  'https://people.googleapis.com/v1/people:createContact?personFields=names' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data '{"names":[{"givenName":"TEST"}]}' \
  --compressed

你知道吗?成功了。

所以我认为这个 gem 不适用于这种特殊情况(它是 google-api-client v: 0.42.2 和 0.43.0 以防你想知道),但如果你来了在这里找到相同问题的解决方案,这对我有用,希望对您有所帮助:

(这将替换我代码中的最后两行):

require 'net/http'
    require 'uri'
    require 'json'

    uri = URI.parse("https://people.googleapis.com/v1/people:createContact?personFields=names")
    request = Net::HTTP::Post.new(uri)
    request.content_type = "application/json"
    request["Authorization"] = "Bearer #{people.authorization.access_token}"
    request["Accept"] = "application/json"
    request.body = JSON.dump({
                                 "names" => [
                                     {
                                         "givenName" => "TEST"
                                     }
                                 ],
                                 "emailAddresses" => [
                                     {
                                         "value" => "testemail@test.com"
                                     }
                                 ],
                                 "phoneNumbers" => [
                                     {
                                         "value" => "12345678"
                                     }
                                 ]
                             }
    )

    req_options = {
        use_ssl: uri.scheme == "https",
    }

    response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
      http.request(request)
    end

    response.code
    response.body

虽然不理想,但今天完成了工作。

编辑:写完这篇文章后我才意识到甚至 'personFields' 参数也没用,因为正文得到了正确的字段。

你可以在我的回答中看到,我只调用了 URI 上的 'names' 字段,但是所有这 3 个字段都正确地存在于我的新联系人(姓名、电子邮件和 phone number) 保存后。所以这也可能是 useless/optional.