简体   繁体   English

Ruby 实现 oauth2 客户端凭据流

[英]Ruby implementing oauth2 client credentials flow

I'm new to Ruby and I'm trying to implement a oauth2 with client credentials flow.我是 Ruby 的新手,我正在尝试使用客户端凭据流实现 oauth2。

I've found the "ouath2" gem, but that requires a redirect_uri that I don't have.我找到了“ouath2” gem,但这需要一个我没有的redirect_uri
Here is the gem .这里是宝石

Here is what I'm trying to implement这是我要实现的

secret_id = 'this-is-a-secret-id'
token_id = 'this-is-a-token-id'
scope = 'such-a-good-scope'
grant_type = 'client_credentials'
@client = nil

# Get access token
def GetAccessToken
    HttpRequest request = HttpRequest::Post("https://awesome-page.com/oauth/token")
    request.content = {
        { "client_id" => token_id },
        { "client_secret" => secret_id }
        { 'grant_type' => grant_type },
        { 'scope' => scope}
    }

    response = request.send
    json = response.content
    accessToken = JsonConvert.DeserializeObject<Token>(json)
    @client = Client.new(bearer: accessToken)
end

# Refresh token
def RefreshToken
    HttpRequest request = HttpRequest::Post("https://awesome-page.com/oauth/token")
    request.content = {
        { "client_id" => token_id },
        { "client_secret" => secret_id }
        { 'grant_type' => grant_type },
        { 'refresh_token' => scope}
    }

    response = request.send
    json = response.content
    accessToken = JsonConvert.DeserializeObject<Token>(json)
    @client = Client.new(bearer: accessToken)
end

# End then implementing the "getting the resources with the client" part and so on...

Any idea how to do this, I'm getting a little bit desperate now任何想法如何做到这一点,我现在有点绝望

Any help is greatly appreciated!任何帮助是极大的赞赏!

You're kind of going about this all wrong.你有点搞错了。 With the Oauth2 gem you need to:使用 Oauth2 gem,您需要:

  1. Initialise a new Oauth2::Client with your client ID, secret, scope, and define the token and redirect url (this is a url in your app that users logging in get sent back to. Not used for Client Credentials flow as it's for server to server comms) Initialise a new Oauth2::Client with your client ID, secret, scope, and define the token and redirect url (this is a url in your app that users logging in get sent back to. Not used for Client Credentials flow as it's for server到服务器通讯)
  2. Call token = client.client_credentials.get_token .调用token = client.client_credentials.get_token This sets token to an AccessToken it obtained.这会将 token 设置为它获得的 AccessToken。
  3. Then call token.get('https://your-url.com/path/to/resource') - or post/patch/delete.然后调用token.get('https://your-url.com/path/to/resource') - 或 post/patch/delete。

Look at the access_token.rb file in the repo to see the methods you can call.查看 repo 中的 access_token.rb 文件以查看您可以调用的方法。 They also take a series of params, for things like additional headers or body payload you can pass.它们还采用一系列参数,例如您可以传递的附加标头或正文有效负载。 It's based on Faraday so you can always look up Faraday docs for help with that part.它基于法拉第,因此您可以随时查找法拉第文档以获取有关该部分的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM