2011-09-21 26 views
5

Tôi đang sử dụng Omniauth để yêu cầu thông tin đăng nhập gmail của người dùng, vì vậy tôi có thể yêu cầu bạn bè/người liên hệ sau này.Cách tạo mã thông báo truy cập trong ruby ​​trên thanh địa chỉ liên hệ gmail

Hiện tại tôi đang nhận danh sách bạn bè bên trong bộ điều khiển OmniauthCallbacks, sử dụng mã thông báo truy cập mà yêu cầu xác thực tạo ra cho tôi. Một cái gì đó như thế này

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 

Làm thế nào tôi có thể sử dụng các thông tin mà tôi đã lưu trữ trong cơ sở dữ liệu để tạo ra một mã thông báo truy cập mới, vì vậy tôi có thể gọi google API từ một bộ điều khiển khác nhau?

+0

De Tester: Bạn có tìm thấy câu trả lời không thể tránh khỏi không? :) Hãy cho chúng tôi biết. –

+0

@DavidJames kiểm tra câu trả lời của tôi. Chỉ cần viết ngày hôm nay và làm việc hoàn hảo vào cuối của tôi .. –

+0

bản sao có thể có của [Whats plugin tốt nhất để tìm kiếm Gmail, Yahoo, Hotmail, Twitter và Facebook danh sách liên lạc trong Ruby on Rails] (http://stackoverflow.com/questions/6311132/whats-the-best-plugin-to-fetch-gmail-yahoo-hotmail-twitter-và-facebook-conta) –

Trả lời

0

Ý tưởng - điều tra xem params[:oauth_verifier] được gửi từ gmail có hoạt động trong các yêu cầu tiếp theo không. Nhưng có lẽ nó chỉ tốt cho một yêu cầu đó.

1

Nhận client_id và client_secret của bạn từ here. Đây là kịch bản thô, hoạt động hoàn toàn tốt. Sửa đổi nó theo nhu cầu của bạn.

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 

Ảnh chụp màn hình cho cài đặt.

enter image description here

Các vấn đề liên quan