2011-09-21 6 views
5

मैं उपयोगकर्ता जीमेल प्रमाण-पत्रों का अनुरोध करने के लिए Omniauth का उपयोग कर रहा हूं, इसलिए मैं बाद में उपयोगकर्ता मित्रों/संपर्कों का अनुरोध कर सकता हूं।जीमेल संपर्कों के लिए रेल पर रूबी में एक्सेस टोकन कैसे बनाएं

अभी मैं प्रमाणीकरण अनुरोध मेरे लिए उत्पन्न होने वाले एक्सेस टोकन का उपयोग करके 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 

की तरह कुछ मैं साख है कि मैं डेटाबेस एक नया पहुँच टोकन बनाने के लिए में संग्रहीत किए गए कैसे उपयोग कर सकते हैं, तो मैं एक अलग नियंत्रक से गूगल एपीआई कॉल कर सकते हैं?

+0

डी परीक्षक: यदि आप एक गैर घिनौने जवाब मिला? :) हमें बताऐ। –

+0

@DavidJames मेरा उत्तर की जाँच करें। बस आज और मेरे अंत में पूरी तरह से काम कर रहे .. की –

+0

संभव डुप्लिकेट लिखा [क्या सबसे अच्छा प्लगइन लाने के लिए जीमेल, याहू, हॉटमेल, ट्विटर और रूबी ऑन रेल्स में फेसबुक संपर्क सूची] (http://stackoverflow.com/questions/6311132/क्या--best-प्लगइन-टू-लाने-gmail-याहू-हॉटमेल-चहचहाना और फेसबुक-conta) –

उत्तर

0

एक विचार - जांच करें कि params[:oauth_verifier] जो जीमेल से भेजा गया था, बाद के अनुरोधों में काम करेगा। लेकिन शायद यह है कि एक अनुरोध के लिए केवल अच्छा है।

1

अपने क्लाइंट_आईडी और क्लाइंट_सेक्रेट को 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

मैं सिर्फ https://github.com/cardmagic/contacts

यह हो रही है जीमेल, याहू, हॉटमेल, आदि से संपर्कों को यह भी उपयोग करने के लिए बहुत आसान है के लिए बहुत अच्छा है का प्रयोग करेंगे।

संबंधित मुद्दे