Ruby OAuth2.0: client credential type has unsuppor

2019-08-29 03:51发布

I am using OAuth2 gem, for making a client_credential authentication. My code is as below,

require 'oauth2'
client = OAuth2::Client.new("my_client_id", "my_client_secret", :site => "my_site_url", :token_url => "oauth2/token")
client.client_credentials.get_token

When I execute above code block, it respond with below error,

OAuth2::Error (invalid_client: Client authentication failed (e.g., unknown client, no client authentication included, or unsupported authentication method))
{
  "error":"invalid_client","error_description":"Client authentication failed (e.g., unknown client, no client authentication included, or unsupported authentication method)",
  "error_hint":"The OAuth 2.0 Client supports client authentication method "client_secret_basic", but method "client_secret_post" was requested. 
  You must configure the OAuth 2.0 client's "token_endpoint_auth_method" value to accept "client_secret_post".","status_code":401}

I checked the using 'net/http' library, and my client_id & client_secrets are valid and working.

The only problem I see is with the authentication method as said in hint of above message,

The OAuth 2.0 Client supports client authentication method "client_secret_basic", but method "client_secret_post" was requested. You must configure the OAuth 2.0 client's "token_endpoint_auth_method" value to accept "client_secret_post"

What I want to know is?

  1. How OAuth2 gem decide on using client_secret_post vs client_secret_basic? I mean How can I request with client_secret_basic in OAuth2 gem?
  2. If not above then, How should I specify token_endpoint_auth_method to accpet client_secret_post?

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-29 04:22

OK, so finally I cleared these points.

  1. OAuth2 gem does make a request to OAuth server with --token_endpoint_auth_method set to 'client_secret_post'.

  2. While registering an client with OAuth server we will have to set token_endpoint_auth_method to 'client_secret_post', so that it will work.

In my case I was using Hydra, so I used below command to create a client:

hydra clients create --endpoint <OAuth server url> --id CLIENT_ID --secret CLIENT_SECRET \
--token-endpoint-auth-method 'client_secret_post' -g client_credentials

Now, using these CLIENT_ID and CLIENT_SECRET with oauth2 works.

But still one point which is unclear - can I make a request with token_endpoint_auth_method set to client_secret_basic using oauth2 gem.

查看更多
登录 后发表回答