简体   繁体   English

通过Ruby On Rails中的API创建Google联系人

[英]Create Google Contact through API in Ruby On Rails

I need some help with Google integration. 我需要有关Google集成的帮助。

I need to create google contacts through the Google Contacts API but all the gems I can find only gets the contents and it also seems like the Google Contacts API doesn't support JSON posts, only xml/atom. 我需要通过Google联系人API创建Google联系人,但我能找到的所有宝石都只能获取内容,而且Google Contacts API似乎不支持JSON帖子,仅支持xml / atom。

This is what I have 这就是我所拥有的

Gemfile 宝石文件

gem 'google-api-client'

google_contacts_controller.rb 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

This all works fine and I am able to get all of my contacts but I can not find a way with this gem or another gem or JSON to create contacts. 一切正常,我可以获取所有联系人,但是我找不到使用该gem或其他gem或JSON创建联系人的方法。

Is there anybody that has had this issue and can please point me in the right direction. 有谁遇到过这个问题,可以请我指出正确的方向。

UPDATE 更新

I have also tried using the google_contacts_api gem to no success. 我也尝试过使用google_contacts_api gem失败。

Here is what I have 这是我所拥有的

Gemfile 宝石文件

gem 'google_contacts_api'

google_contacts_controller.rb 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

Everything works fine until this line (which I got out of the example supplied for the gem): 在此行之前一切正常(我从提供给gem的示例中退出):

new_group = google_contacts_user.create_group('New group')

Then I get this line exception: 然后我得到此行异常:

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

Can someone spot my mistake I made? 有人可以发现我犯的错误吗?

Also, how would I proceed to create contacts as I can not seem to find documentation or posts on actually creating and updating contacts, I thought maybe I would have to create a group and then I can add contacts to the group but as I can not manage to create a group I can not test that theory. 另外,我将如何继续创建联系人,因为我似乎找不到有关实际创建和更新联系人的文档或帖子,我想也许我必须创建一个组,然后才能将联系人添加到该组中,但是我无法设法建立一个小组,我无法检验该理论。

Please point me in the right direction 请指出正确的方向

The Google People API is read-only and can't be used to update or add new contacts. Google People API是只读的,不能用于更新或添加新的联系人。 For that, you need to use the Google Contacts API which is, unfortunately, not supported by the google-api-client gem. 为此,很遗憾,您需要使用Google Contacts API,而google-api-client gem不支持该google-api-client

To access the Google Client API, you can try using this gem: 要访问Google Client API,您可以尝试使用以下gem:

google_contacts_api google_contacts_api

If you run into trouble setting it up, check out this stackoverflow question: 如果您在设置它时遇到麻烦,请查看以下stackoverflow问题:

access google contacts api 访问Google通讯录API

It has a helpful answer written by the same person who wrote the gem. 它具有由撰写该gem的同一人撰写的有用答案。

EDIT 编辑

It looks like development on the google_contacts_api gem stopped before functionality for creating contacts was added. 在添加用于创建联系人的功能之前,似乎已经停止了对google_contacts_api gem的开发。 I'm looking through the various gems on Github and they're all in states of disrepair / death. 我正在查看Github上的各种宝石,它们都处于失修/死亡的状态。

As much as it pains me to say it, your best bet might be to access Google Contacts directly via their web API . 尽我所能说的话,您最好的选择是直接通过其Web API访问Google联系人。

Best of luck and sorry for the disheartening answer. 祝您好运,对不起您的答复,我们深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM