简体   繁体   English

使用令牌对API进行身份验证

[英]Authenticating to an API with a token

I'm working with the Zendesk API, an HTTPS-only, JSON API and authentication is required to update a resource, and the API supplies an API token to use when authenticating as different users. 我正在使用Zendesk API,一个仅限HTTPS,JSON API并且需要身份验证来更新资源,并且API提供了在作为不同用户进行身份验证时使用的API令牌。 When updating a resource, I issue a PUT request over SSL, assign the appropriate JSON content to the request body, and specify the Content-Type request header as application/json . 更新资源时,我通过SSL发出PUT请求,将相应的JSON内容分配给请求正文,并将Content-Type请求标头指定为application/json

Next, the API instructs its users to authenticate as the end-user by either using the user's email and password (which I can't do for several reasons) or to use the user's email address along with the API token. 接下来,API通过使用用户的电子邮件和密码(由于多种原因我无法做到)或者使用用户的电子邮件地址以及API令牌来指示其用户作为最终用户进行身份验证。 The following is my attempt to authorize to the API with the Authorization header: 以下是我尝试使用Authorization标头授权API:

@id = params[:id]
@comment_body = params[:comment]

uri = URI.parse "https://{subdomain}.zendesk.com/api/v2/requests/#{@id}.json"
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Put.new(uri.request_uri)
req.body = '{"request": {"comment":{"value":' + "\"#{@comment_body}\"" + '}}}'
req['Content-Type'] = 'application/json'

#The following two lines do not work!
credentials = Base64::encode64("{user_email}/token:{api_token}")
request.headers['Authorization'] = "Basic #{credentials}"

response = http.request(req)

The API specifies that the format for authentication using the API token is {user_email}/token:{api_token} . API指定使用API​​令牌进行身份验证的格式为{user_email}/token:{api_token} I encoded that format with Base64::encode64 and passed it to the Authorization Header preceded with Basic , but the response is a 401 Unauthorized. 我使用Base64::encode64对该格式进行了编码,并将其传递给了Basic之前的Authorization Header,但响应是401 Unauthorized。 However, replacing those two lines with req.basic_auth {user_email}, {user_password} works fine. 但是,使用req.basic_auth {user_email}, {user_password}替换这两行req.basic_auth {user_email}, {user_password}可以正常工作。

So my question is, how can I authenticate as a different user using the email and the given API token as authentication instead of supplying the user's email and password to req.basic_auth ? 所以我的问题是,如何使用电子邮件和给定的API令牌作为身份验证,而不是将用户的电子邮件和密码提供给req.basic_auth作为不同的用户进行身份验证?

The googling I've done on the topic has revealed very little; 我在这个主题上所做的谷歌搜索显示很少; apparently it's a lot more common to use the normal {username}:{password} format when dealing with the Authorization header than an API token. 显然,在处理Authorization标头时使用普通的{username}:{password}格式比使用API​​令牌更常见。

Thanks in advance!! 提前致谢!!

Update: Weirdly, trying to authenticate as the end-user with req['Authorization'] = "Basic #{credentials}" does not return a 401 Unauthorized Error or a WWW-Authenticate header while trying to authorize as request.headers['Authorize'] = "Basic #{credentials}" does. 更新:奇怪的是,在尝试授权为request.headers['Authorize'] = "Basic #{credentials}"时,尝试使用req['Authorization'] = "Basic #{credentials}"作为最终用户进行身份验证时,不会返回401 Unauthorized Error或WWW-Authenticate标头request.headers['Authorize'] = "Basic #{credentials}"确实如此。

Finally figured it out after much head-banging and nearly throwing my laptop out the window. 经过多次头撞,几乎把我的笔记本电脑扔到了窗外,终于弄明白了。 Suddenly, the answer seems incredibly obvious. 突然间,答案似乎非常明显。

When using Net::HTTP, its basic_auth method can also accept tokens depending on the API, and the Zendesk API specifies that the format for using the API token is {email}/token:{token}. 使用Net :: HTTP时,其basic_auth方法也可以接受令牌,具体取决于API,Zendesk API指定使用API​​令牌的格式为{email} / token:{token}。 Basic authentication uses the format {username}:{password}, where the two fields are separated by a colon , meaning in Zendesk's case I can place {email}/token as the first argument and {token} as the second argument (instead of the username as the first argument and the password as the second argument), so the following code is correct: 基本身份验证使用格式{username}:{password},其中两个字段用冒号分隔 ,这意味着在Zendesk的情况下,我可以将{email} / token作为第一个参数,{token}作为第二个参数(而不是用户名作为第一个参数,密码作为第二个参数),因此以下代码是正确的:

req.basic_auth "{email}/token", "{api_token}"

I hope anyone who found this useful could leave a comment. 我希望任何发现这个有用的人都可以发表评论。 Nice to know I spared someone from this frustration. 很高兴知道我从这个挫折中饶过一个人。

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

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