我需要Google集成的一些幫助。通過API在Ruby On Rails上創建Google聯繫人
我需要通過Google Contacts API創建Google聯繫人,但是我可以找到的所有寶石都只能獲取內容,而且Google Contacts API不支持JSON帖子,只有xml/atom。
這是我
的Gemfile
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
這一切工作正常,我能得到我的所有聯繫人,但我找不到用這個寶石或其他寶石或JSON來創建聯繫人的方法。
有沒有人有這個問題,可以請指點我在正確的方向。
UPDATE
我也使用google_contacts_api寶石沒有成功嘗試。
這裏是我有什麼
的Gemfile
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
一切正常對於寶石):
new_group = google_contacts_user.create_group('New group')
然後我得到這一行例外:
undefined method `create_group' for #<GoogleContactsApi::User:0x007ff2da2291b8>
可有人發現我的錯誤我犯了?
此外,我將如何繼續創建聯繫人,因爲我似乎無法找到關於實際創建和更新聯繫人的文檔或帖子,我想也許我必須創建一個組,然後才能將聯繫人添加到該組中因爲我無法創建一個組,所以我無法測試這個理論。
請點我在正確的方向
感謝您的回答,但我已經發現該帖子並嘗試使用該寶石,我也設法設置並授權它,並獲得聯繫人列表,但也無法創建聯繫人 – IDP
在這種情況下,它理應更新你的問題。發佈你的新代碼,我會努力更新我的答案。 – eiko
我會盡快答覆 – IDP