【问题标题】:How to get create an access token in ruby on rails for gmail contacts如何在 ruby​​ on rails 中为 gmail 联系人创建访问令牌
【发布时间】:2011-11-21 20:44:22
【问题描述】:

我正在使用 Omniauth 请求用户 gmail 凭据,因此我可以稍后请求用户朋友/联系人。

现在,我正在使用身份验证请求为我生成的访问令牌在 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?

【问题讨论】:

标签: ruby-on-rails ruby gmail omniauth google-contacts-api


【解决方案1】:

here 获取您的 client_id 和 client_secret。这是一个粗略的脚本,效果很好。根据您的需要对其进行修改。

    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

设置屏幕截图。

【讨论】:

    【解决方案2】:

    一个想法 - 调查从 gmail 发送的 params[:oauth_verifier] 在后续请求中是否有效。但也许它只对那一个请求有好处。

    【讨论】:

      【解决方案3】:

      我只会使用https://github.com/cardmagic/contacts

      非常适合从 gmail、yahoo、hotmail 等获取联系人。使用起来也非常简单。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-22
        • 2021-07-06
        • 1970-01-01
        • 1970-01-01
        • 2016-04-14
        • 2017-11-03
        • 1970-01-01
        • 2018-09-01
        相关资源
        最近更新 更多