【问题标题】:Create Google Contact through API in Ruby On Rails在 Ruby On Rails 中通过 API 创建 Google 联系人
【发布时间】:2017-05-13 03:14:53
【问题描述】:

我需要 Google 集成方面的帮助。

我需要通过 Google Contacts API 创建 google 联系人,但我能找到的所有 gem 都只能获取内容,而且 Google Contacts API 似乎不支持 JSON 帖子,只支持 xml/atom。

这就是我所拥有的

宝石文件

gem 'google-api-client'

google_contacts_controller.rb

require 'google/apis/people_v1'
require 'google/api_client/client_secrets'

class GoogleContactsController < ApplicationController

  def auth
    client_secrets = Google::APIClient::ClientSecrets.load('app/controllers/google_contacts/client_secret.json')
    auth_client = client_secrets.to_authorization
    auth_client.update!(
      :scope => 'https://www.google.com/m8/feeds/',
      :redirect_uri => 'http://localhost:3000/google_contacts/auth')[]
    if request['code'] == nil
      auth_uri = auth_client.authorization_uri.to_s
      redirect_to auth_uri
    else
       auth_client.code = request['code']
       auth_client.fetch_access_token!
       auth_client.client_secret = nil
       _auth_client(auth_client)
       redirect_to action: 'sync_contacts'
    end
  end

  def sync_contacts
    unless session.has_key?(:credentials)
      _auth_client
      unless session.has_key?(:credentials)
        redirect action: 'auth'
      end
    end
   client_opts = JSON.parse(session[:credentials])

   auth_client = Signet::OAuth2::Client.new(client_opts)

   people_service = Google::Apis::PeopleV1::PeopleServiceService.new
   people_service.authorization = auth_client

   response = people_service.list_person_connections('people/me', request_mask_include_field: %w'person.names', page_size: 10,)
   remote_name_arry = response.connections.map{|person| person.names[0].display_name}

    redirect_to action: 'done', message: 'Synced Contacts'
  end

  def done
    @message = params[:message]
  end

  private
  def _auth_client(auth_client=nil)
    if auth_client
      credentials = GoogleContactCredential.new
      credentials.authorization_uri = auth_client.authorization_uri
      credentials.token_credential_uri = auth_client.token_credential_uri
      credentials.client_id = auth_client.client_id
      credentials.scope = "[#{auth_client.scope.join(', ')}]"
      credentials.redirect_uri = auth_client.redirect_uri
      credentials.expiry = auth_client.expiry
      credentials.expires_at = auth_client.expires_at
      credentials.access_token = auth_client.access_token
      if credentials.save
        session[:credentials] = credentials.to_json
      end
    else
      credentials = GoogleContactCredential.first
      if credentials.present?
        session[:credentials] = credentials.to_json
      end
    end
    credentials
  end
end

这一切都很好,我可以获取我的所有联系人,但我找不到使用此 gem 或其他 gem 或 JSON 的方法来创建联系人。

有没有人遇到过这个问题,请指点我正确的方向。

更新

我也尝试过使用 google_contacts_api gem,但没有成功。

这就是我所拥有的

宝石文件

gem 'google_contacts_api'

google_contacts_controller.rb

def sync_contacts
  unless session.has_key?(:credentials)
    _auth_client
    unless session.has_key?(:credentials)
      redirect action: 'auth'
    end
  end
  client_opts = JSON.parse(session[:credentials])

  auth = OAuth2::Client.new(client_opts['client_id'], client_opts['client_secret'], client_opts)

  token = OAuth2::AccessToken.new(auth, client_opts['access_token'])

  google_contacts_user = GoogleContactsApi::User.new(token)
  contacts = google_contacts_user.contacts

  contact = contacts.first

  logger.info contact.title

  new_group = google_contacts_user.create_group('New group')

  redirect_to action: 'done', message: 'Synced Contacts'
end

在这一行之前一切正常(我从为 gem 提供的示例中得出):

new_group = google_contacts_user.create_group('New group')

然后我得到这行异常:

undefined method `create_group' for #<GoogleContactsApi::User:0x007ff2da2291b8>

有人能看出我犯的错误吗?

另外,我将如何继续创建联系人,因为我似乎无法找到有关实际创建和更新联系人的文档或帖子,我想也许我必须创建一个组,然后我可以将联系人添加到组中,但作为我无法创建一个无法测试该理论的小组。

请指出正确的方向

【问题讨论】:

    标签: ruby-on-rails json ruby google-api-client rails-4-2-1


    【解决方案1】:

    Google People API 是只读的,不能用于更新或添加新联系人。为此,您需要使用 google-api-client gem 不支持的 Google Contacts API。

    要访问 Google Client API,您可以尝试使用此 gem:

    google_contacts_api

    如果您在设置时遇到问题,请查看这个 stackoverflow 问题:

    access google contacts api

    它有一个有用的答案,由编写 gem 的人写。

    编辑

    在添加创建联系人的功能之前,google_contacts_api gem 的开发似乎停止了。我正在浏览 Github 上的各种宝石,它们都处于年久失修/死亡的状态。

    尽管说起来让我很痛苦,但最好的办法可能是直接通过 their web API 访问 Google 通讯录。

    祝你好运,对令人沮丧的答案表示抱歉。

    【讨论】:

    • 感谢您的回答,但我已经找到该帖子并尝试使用 gem,我还设法设置并授权它并获取联系人列表但也无法创建联系人
    • 在这种情况下,您应该更新您的问题。发布您的新代码,我会努力更新我的答案。
    • @IDP 我不确定为什么 create_group 不起作用,但这似乎是一个有争议的问题,因为 gem 本身是不完整的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多