gpt4 book ai didi

ruby-on-rails - Rails,OmniAuth,google_oauth2,google-api-client,Moments.insert ... 401未经授权...为什么?

转载 作者:行者123 更新时间:2023-12-04 04:30:59 25 4
gpt4 key购买 nike

除了使用Google+登录按钮外,我目前无法在网上找到答案,因为我现在不想使用Java +登录按钮,因为我不想使用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”]的回调,并且我希望获得的所有信息,包括 token ,电子邮件地址等。我从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)

但是没有运气。

我认为这个问题与以下方面有关:
  • OmniAuth没有向Google正确发出request_visible_actions
  • 我没有在Google::APIClient对象中正确设置 token

  • 到目前为止,我已经使用了以下资源,但是我正式陷入困境:
  • http://blog.baugues.com/google-calendar-api-oauth2-and-ruby-on-rails
  • https://developers.google.com/+/api/latest/moments/insert

  • 任何想法,将不胜感激!

    最佳答案

    这是我使用'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

    关于ruby-on-rails - Rails,OmniAuth,google_oauth2,google-api-client,Moments.insert ... 401未经授权...为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16453252/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com