簡體   English   中英

Rails,OmniAuth,google_oauth2,google-api-client,Moments.insert ...... 401未經授權......為什么?

[英]Rails, OmniAuth, google_oauth2, google-api-client, Moments.insert… 401 unauthorized… why?

我無法在網上找到這個答案 - 除了使用Google+登錄按鈕,此時我不想使用,因為我不想進入Javascript,如果我不這樣做不得不。

我有一個Ruby on Rails應用程序(ruby v1.9.3,rails v3.2.13),其中我連接了OmniAuth,我正在使用google_oauth2 gem與Google+集成。

我的簡單目標是允許用戶通過Google+進行身份驗證,授予對我的Google API項目的訪問權限,然后使用google-api-client gem向Google+用戶的保險庫發布片刻。

我已經設置了Google API項目,為Web應用程序創建了OAuth 2.0,並啟用了Google+ API服務。

我已經使用以下提供程序設置了OmniAuth,並且我添加了request_visible_actions選項以允許我發布(我認為這是正確的但是沒有看到我在網上看過的任何代碼示例中使用過這個...):

provider :google_oauth2, CLIENT_ID, CLIENT_SECRET, {
    access_type: 'offline',
    scope: 'userinfo.email,userinfo.profile,plus.me,https://www.googleapis.com/auth/plus.login',
    request_visible_actions: 'http://schemas.google.com/AddActivity',
    redirect_uri: 'http://localhost/auth/google_oauth2/callback'
}

當我將用戶重定向到/ auth / google_oauth2時,它會將用戶發送到Google+以授權我的應用,當用戶批准時,它會返回到我的回調,我可以訪問request.env [“omniauth.auth”]並且它具有我期望的所有信息,包括令牌,電子郵件地址等。我正在存儲來自auth [“credentials”] [“token”]的access_token。

到目前為止一切都那么好吧?

當我嘗試使用以下代碼發布時刻時,我遇到一個異常,表示401未經授權的錯誤。

client = Google::APIClient.new

client.authorization.access_token = self.access_token

plus = client.discovered_api('plus', 'v1')

moment = {
    :type => 'http://schemas.google.com/AddActivity',
    :target => { :id => Time.now.to_i.to_s,
               :description => message,
               :name => message
    }
}

# post a moment/activity to the vault/profile
req_opts = { :api_method => plus.moments.insert,
             :parameters => { :collection => 'vault', :userId => 'me', },
             :body_object => moment
}

response = client.execute!(req_opts).body

我也試過更換

client.authorization.access_token = self.access_token

credentials = Hash.new
credentials[:access_token] = self.access_token
credentials[:refresh_token] = self.refresh_token
credentials[:expires_at] = self.expires_at
client.authorization.update_token!(credentials)

但沒有運氣。

我認為這個問題要么與以下問題有關:

  1. OmniAuth沒有正確地向Google發出request_visible_actions
  2. 我沒有正確地在Google :: APIClient對象中設置令牌

我已經使用以下資源做到這一點,但我正式陷入困境:

任何想法,將不勝感激!

以下是我使用'omniauth-google-oauth2'和'google-api-client'在網絡應用中運行的代碼。 此示例代碼使用日歷API,但我想它會對您有用。

require 'google/api_client'

class Calendar
  def initialize(user)
    @user = user
  end

  def events
    result = api_client.execute(:api_method => calendar.events.list,
                            :parameters => {'calendarId' => 'primary'},
                            :authorization => user_credentials)

    result.data
  end

  private

  def api_client
    @client ||= begin
      client = Google::APIClient.new(application_name: 'xxx', application_version: '0.0.1')
      client.authorization.client_id = ENV["GOOGLE_KEY"]
      client.authorization.client_secret = ENV["GOOGLE_SECRET"]
      client.authorization.scope = 'https://www.googleapis.com/auth/calendar'
      client
    end
  end

  def calendar
    @calendar ||= api_client.discovered_api('calendar', 'v3')
  end

  def user_credentials
    auth = api_client.authorization.dup
    # @user.credentials is an OmniAuth::AuthHash  cerated from request.env['omniauth.auth']['credentials']
    auth.update_token!(access_token: @user.credentials.token) 
    auth
  end
end

在API控制台中,您是注冊為Web應用程序還是已安裝的應用程序? 我認為對於您的情況,您必須選擇已安裝的應用程序,以便在用戶不在線時令牌有效。

嘗試使用plus.login更改https://www.googleapis.com/auth/plus.login 它以相同的設置為我工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM