私はGoogleとの統合について助けが必要です。Ruby On RailsでGoogleコンタクトをAPIで作成する
私はGoogle Contacts APIを使ってGoogleの連絡先を作成する必要がありますが、私が見つけることができるすべての宝石は内容を取得するだけで、Google Contacts APIはJSON投稿をサポートしていないようです。
これは私が
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>
誰かが私が作った私のミスを見つけることができますか?
また、実際に連絡先を作成して更新する際にドキュメントや投稿を見つけることができないため、連絡先の作成を進めるには、グループを作成してからグループに連絡先を追加できますグループを作ることができないので、その理論をテストすることはできません。
はGoogleの人々APIは読み取り専用で更新したり、新しい連絡先を追加するために使用することはできません右方向
回答はありがたいですが、私はすでにその投稿を見つけて、宝石の使用を試みました。私はそれを設定し、それを承認して連絡先リストを取得しましたが、連絡先も作成できませんでした。 – IDP
その場合、あなたの質問を更新する必要があります。新しいコードを投稿すると、私は自分の答えを更新するつもりです。 – eiko
私はすぐにお礼を言います – IDP