Signet Oauth2 Basic Auth无法使用Get Response api

时间:2018-04-09 13:43:08

标签: ruby-on-rails ruby oauth-2.0 google-oauth2 getresponse

我正在尝试使用Signet :: Oauth2 :: Client从getresponse.com获取访问令牌

client = Signet::OAuth2::Client.new(
  client_id: GET_RESPONSE_CLIENT_ID,
  client_secret: GET_RESPONSE_CLIENT_SECRET,
  token_credential_uri: 'https://api.getresponse.com/v3/token',
  redirect_uri: my_callback_uri,
  grant_type: 'authorization_code',
  code: the_code_i_got_from_get_response
)
response = client.fetch_access_token!

但是Get Response总是返回:

{"error"=>"invalid_client", "error_description"=>"Client credentials were not found in the headers"}

我很容易用curl完成这个请求。它与令牌成功返回。

curl -v -u client_id:client_secret https://api.getresponse.com/v3/token -d "grant_type=authorization_code&code=abc123thisisthecode&redirect_uri=https://myserver.com/callback"

我已经尝试了一百万件事并阅读了客户端来源,但我没有看到我想做错了。我将继续阅读文档,但与此同时我想我会在stackoverflow问这里的专业人士。有人知道答案吗?这是api文档https://apidocs.getresponse.com/v3/oauth2

1 个答案:

答案 0 :(得分:0)

遇到类似问题。我按照以下步骤为我工作。

Step.1加载从Google开发者控制台获取的凭据。

  client_secrets = Google::APIClient::ClientSecrets.load("#{Rails.root}/zenledger_ga_secrets.json")
  auth_client = client_secrets.to_authorization

  auth_client.update!(
    :scope => 'email profile https://www.googleapis.com/auth/analytics.readonly',
    :grant_type => "authoriation_code",
    :response_type => 'code',
    :redirect_uri => 'http://localhost:3001/admin/oauth2callback',
    :additional_parameters => {
      "access_type" => "offline",         # offline access
      "include_granted_scopes" => "true",  # incremental auth
    }
  )

第2步。加载您的Signet客户端以获取访问所需API所需的访问令牌。

    @client = Signet::OAuth2::Client.new(
    :authorization_uri => 'https://accounts.google.com/o/oauth2/auth',
    :token_credential_uri =>  'https://oauth2.googleapis.com/token',
    :client_id => ''xxxxxxxxxx',
    :client_secret => 'xxxxxxxxxx',
    :scope => 'email profile',
    :redirect_uri => 'http://localhost/admin/oauth2callback'
  )
  @client.code = auth_code
  @client.fetch_access_token!
  

请注意auth_code由Google在重定向URL中发送。您可以在应用程序的oauth2callback网址中将它们作为参数获取。