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

问题描述

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

我设法使其他方法正常工作(list_person_connections,get_people,delete_person_contact和update_person_contact),但是我无法从google people API中获得createContact(create_person_contact)方法

发送帖子后,出现此错误

400 Caught error badRequest: Request contains an invalid argument.

这是一个示例代码,用于仅使用名称字段来创建联系人(我实际上还希望在正文上发送email和phoneNumbers参数,但它也会返回相同的错误,因此我为您提供了最简单的示例这里):

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

无论我如何更改,它都会返回上述错误。 在文档中,您实际上可以尝试测试完全相同的代码,并且可以正常工作。

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选项。因此,我从上面链接的“尝试此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上调用了“姓名”字段,但是保存后,新联系人(姓名,电子邮件和电话号码)中的那三个字段都正确存在。所以这可能也没用/可选。