如何让我的谷歌oauth尝试并在特定路线上授权应用程序

时间:2017-05-30 21:45:37

标签: ruby oauth sinatra

我有一个谷歌Oauth,当用户访问我的网页时会让用户授权,但是我只希望他们必须授权该应用,以便我可以在访问某个页面时获取他们的访问权限并刷新令牌输入谷歌api信息。谷歌正在授权他们无论他们在如何阻止这个想法的任何想法.Ruby不会让我在路线中的任何一个。

def user_credentials
# Build a per-request oauth credential based on token stored in 
session
# which allows us to use a shared API client.
@authorization ||= (
auth = settings.authorization.dup
auth.redirect_uri = to('/oauth2callback')
auth.update_token!(session)
auth
)
end

configure do

 Google::Apis::ClientOptions.default.application_name = 'Get Login 
 info for Google Ad Exchange'
 Google::Apis::ClientOptions.default.application_version = '1.0.0'

 client_secrets = Google::APIClient::ClientSecrets.load
 authorization = client_secrets.to_authorization
 authorization.scope = 
 'https://www.googleapis.com/auth/adexchange.seller.readonly'

  set :authorization, authorization
end

before do
# Ensure user has authorized the app
unless user_credentials.access_token || request.path_info =~ 
/^\/oauth2/
redirect to('/oauth2authorize')
end
end


 after do
 # Serialize the access/refresh token to the session and credential 
 store.
# We could potentially need to pull back the client_id and 
client_secret as well and add them to the dynamo database.

#   session[:client_id] = user_credentials.client_id
#   session[:client_secret] = user_credentials.client_secret
    session[:access_token] = user_credentials.access_token
    session[:refresh_token] = user_credentials.refresh_token
    session[:expires_in] = user_credentials.expires_in
    session[:issued_at] = user_credentials.issued_at

  end

get '/oauth2authorize' do
 # Request authorization
 redirect user_credentials.authorization_uri.to_s, 303
 end

get '/oauth2callback' do
 # Exchange token
 user_credentials.code = params[:code] if params[:code]
 user_credentials.fetch_access_token!
 redirect to('/')

  end

1 个答案:

答案 0 :(得分:0)

想出来,意思是提前发布,但在这篇文章上发了警告,所以我想知道我们做了什么来更新它的工作。

 get '/googleauth' do
   salesforce_username = params[:salesforce_username] || ''
   unless session.has_key?(:credentials)
   redirect to('/oauth2callback')
 end
   client_opts = JSON.parse(session[:credentials])
   auth_client = Signet::OAuth2::Client.new(client_opts)
   redirect to('/googleadx')
 end

 get '/oauth2callback' do
  client_secrets = Google::APIClient::ClientSecrets.load
  auth_client = client_secrets.to_authorization
  auth_client.update!(
  :scope => 'https://www.googleapis.com/auth/adexchange.seller.readonly',
:redirect_uri => url('/oauth2callback'))
 if request['code'] == nil
  auth_uri = auth_client.authorization_uri.to_s
  redirect to(auth_uri)
 else
  auth_client.code = request['code']
  auth_client.fetch_access_token!
  session[:access_token] = auth_client.access_token
  session[:refresh_token] = auth_client.refresh_token
  session[:expires_in] = auth_client.expires_in
  session[:issued_at] = auth_client.issued_at
  auth_client.client_secret = nil
  session[:credentials] = auth_client.to_json
  redirect to('/googleadx')
end
end

get '/googleadx' do

# configure()

if params[:username]
    successmessage = params[:username] + "'s credentials added successfully."
else
    message = ''
end

salesforce_username = session[:salesforce_username] || ''
access_token = session[:access_token]
refresh_token = session[:refresh_token]
googleDollarLimit = ''

erb :googleadx, locals: {message: message, successmessage: successmessage, salesforce_username: salesforce_username, access_token: access_token, refresh_token: refresh_token, googleDollarLimit: googleDollarLimit}
end