2011-09-21 12 views
5

Omniauthを使用してユーザーのGmail資格情報を要求しているので、後で友人や連絡先を要求できます。gmailの連絡先のruby on accessトークンを作成する方法

今、認証要求が生成するアクセストークンを使用して、友人リストをOmniauthCallbacksコントローラの中に入れています。このような何か

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 

    def google 
    auth = env["omniauth.auth"] 
    gmail_contacts 
    .... 
    end 

    ..... 
    protected 
    def gmail_contacts 
    access_token = env["omniauth.auth"]['extra']['access_token'] 
    response = access_token.request(:get, 
     "https://www.google.com/m8/feeds/contacts/default/full?max-results=10000") 
    ..... 
    end 
end 

どのように私は新しいアクセストークンを作成するために、私は、データベースに格納されている資格情報を使用することができますので、私は別のコントローラからGoogleのAPIを呼び出すことができますか?

+0

De Tester:あなたは嫌な答えを見つけましたか? :) 我々に教えてください。 –

+0

@DavidJamesは私の答えをチェックします。ちょうど今日書いて、私の最後で完璧に働いています。 –

+0

[Ruby on RailsでGmail、Yahoo、Hotmail、Twitter、Facebookのコンタクトリストを取得するのに最適なプラグインは何ですか?](http://stackoverflow.com/questions/6311132/whats-the-best-plugin-to-fetch-gmail-yahoo-hotmail-twitter-and-facebook-conta) –

答えて

0

gmailから送信されたparams[:oauth_verifier]がその後のリクエストで機能するかどうかを調査します。しかし、たぶんそれはその1つの要求にのみ良いです。

1

クライアントIDとクライアントIDをhereから取得します。これは大雑把なスクリプトで、完璧に動作します。お客様のニーズに合わせて変更しました。

require 'net/http' 
    require 'net/https' 
    require 'uri' 
    require 'rexml/document' 

    class ImportController < ApplicationController 

     def authenticate 
     @title = "Google Authetication" 

     client_id = "xxxxxxxxxxxxxx.apps.googleusercontent.com" 
     google_root_url = "https://accounts.google.com/o/oauth2/auth?state=profile&redirect_uri="+googleauth_url+"&response_type=code&client_id="+client_id.to_s+"&approval_prompt=force&scope=https://www.google.com/m8/feeds/" 
     redirect_to google_root_url 
     end 

     def authorise 
     begin 
      @title = "Google Authetication" 
      token = params[:code] 
      client_id = "xxxxxxxxxxxxxx.apps.googleusercontent.com" 
      client_secret = "xxxxxxxxxxxxxx" 
      uri = URI('https://accounts.google.com/o/oauth2/token') 
      http = Net::HTTP.new(uri.host, uri.port) 
      http.use_ssl = true 
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
      request = Net::HTTP::Post.new(uri.request_uri) 

      request.set_form_data('code' => token, 'client_id' => client_id, 'client_secret' => client_secret, 'redirect_uri' => googleauth_url, 'grant_type' => 'authorization_code') 
      request.content_type = 'application/x-www-form-urlencoded' 
      response = http.request(request) 
      response.code 
      access_keys = ActiveSupport::JSON.decode(response.body) 

      uri = URI.parse("https://www.google.com/m8/feeds/contacts/default/full?oauth_token="+access_keys['access_token'].to_s+"&max-results=50000&alt=json") 

      http = Net::HTTP.new(uri.host, uri.port) 
      http.use_ssl = true 
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
      request = Net::HTTP::Get.new(uri.request_uri) 
      response = http.request(request) 
      contacts = ActiveSupport::JSON.decode(response.body) 
      contacts['feed']['entry'].each_with_index do |contact,index| 

      name = contact['title']['$t'] 
      contact['gd$email'].to_a.each do |email| 
       email_address = email['address'] 
       Invite.create(:full_name => name, :email => email_address, :invite_source => "Gmail", :user_id => current_user.id) # for testing i m pushing it into database.. 
      end 

      end 
     rescue Exception => ex 
      ex.message 
     end 
     redirect_to root_path , :notice => "Invite or follow your Google contacts." 


     end 

    end 

設定のスクリーンショット

enter image description here

0

私はちょうどそれが使用することも非常に簡単ですなどのGmail、ヤフー、ホットメール、から連絡先を取得するための素晴らしいことだhttps://github.com/cardmagic/contacts

を使用します。

関連する問題