REST API POST 调用的 (OAuth) 授权请求标头中的不记名令牌 [英] Bearer token in the (OAuth) Authorization request header for REST API POST call

查看:124
本文介绍了REST API POST 调用的 (OAuth) 授权请求标头中的不记名令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想弄清楚如何为 REST API POST 调用执行此 OAuth 授权令牌.

Hey all i am trying to figure out how to do this OAuth authorization token for a REST API POST call.

文件说明:

With a valid access token, your app can make calls to any Yammer API endpoint by sending the access token as a "Bearer" token in the "Authorization" request header.

GET /api/v1/messages/following.json HTTP/1.1 
Host: www.yammer.com 
Authorization: Bearer abcDefGhiFor

more details on the "Bearer" token refer to [enter link description here][1] 

If the access token expires or the user de-authorizes your app, the API request will return an HTTP 401 with the following error in the body of the response.

{
  "response": {
    "message": "Token not found.",
    "code": 16,
    "stat": "fail"
  }
}

如果发生此错误,您的应用可以通过重新运行适当的流程来请求新的访问令牌.

Your app can request a new access token by re-running the appropriate flow if this error occurs.

目前我的 VB.net 代码是这样的:

Currently my VB.net code is this:

Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Dim address As Uri
Dim data As StringBuilder
Dim byteData() As Byte
Dim postStream As Stream = Nothing

address = New Uri("https://www.yammer.com/api/v1/messages.json")
request = DirectCast(WebRequest.Create(address), HttpWebRequest)

request.Method = "POST"
request.Headers("Authorization") = "Bearer " & yammerAPI.userToken
request.ContentType = "application/json"
request.Host = "www.yammer.com"

Dim body As String = "test"
Dim replied_to_id As Integer = 123456789
Dim group_id As Integer = 123456789

data = New StringBuilder()
'data.Append("&replied_to_id=" & HttpUtility.UrlEncode(replied_to_id))
data.Append("group_id=" & HttpUtility.UrlEncode(group_id))
data.Append("&body=" & HttpUtility.UrlEncode(body))

byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())
request.ContentLength = byteData.Length

Try
   postStream = request.GetRequestStream()
   postStream.Write(byteData, 0, byteData.Length)
Finally
   If Not postStream Is Nothing Then postStream.Close()
End Try

Try
   response = DirectCast(request.GetResponse(), HttpWebResponse)
   reader = New StreamReader(response.GetResponseStream())
   Debug.Print(reader.ReadToEnd())
Finally
   If Not response Is Nothing Then response.Close()
End Try

我不断收到以下错误:远程服务器返回错误:(401) 未经授权.

我在以下 Stackoverflow 帖子中发现了这一点:

I found this in a following Stackoverflow posting:

Yammer API 要求 OAuth 数据位于标头中.如果您查看他们的获取数据示例,您会看到请求如下所示.

The Yammer API requires the OAuth data to be in the header. If you look at their example for Getting Data, you'll see the request looks like.

GET/api/v1/messages/favorites_of/1234 HTTP/1.1主持人:www.yammer.com

GET /api/v1/messages/favorites_of/1234 HTTP/1.1 HOST: www.yammer.com

授权:OAuth的oauth_consumer_key = KsTROcNF1Fx3e1PwA",组oauth_token = vlVH7A7DOm9wXuHdv58A",oauth_signature_method = PLAINTEXT",oauth_timestamp = 1297383841092",oauth_nonce = 1047685618",oauth_verifier = E4F8",oauth_signature = yPsEvDnNPIA8xGCFLvMJ73K0DD9ivMpATJeFOSo%26fSFh9UPkHQ6oRwK5OTne33ltnSnbQ9XrAhA72heg"

Authorization: OAuth oauth_consumer_key="KsTROcNF1Fx3e1PwA",oauth_token="vlVH7A7DOm9wXuHdv58A",oauth_signature_method="PLAINTEXT",oauth_timestamp="1297383841092",oauth_nonce="1047685618",oauth_verifier="E4F8",oauth_signature="yPsEvDnNPIA8xGCFLvMJ73K0DD9ivMpATJeFOSo%26fSFh9UPkHQ6oRwK5OTne33ltnSnbQ9XrAhA72heg"

OAuth 数据位于授权标头中,而不是在 URL 中.URL 中有任何 OAuth 数据的唯一时间是在您进行授权时.

The OAuth data is in the Authorization header and not in the URL. The only time you have any OAuth data in the URL is when you do the authorize.

任何帮助都有助于更好地理解这一点!

Any help would be great to understand this more!

推荐答案

我最近使用 Oauth 的经验表明内容类型应该是:

My recent experience with Oauth suggests the content type should be:

Request.ContentType = "application/x-www-form-urlencoded"Request.Method = "POST"Request.ContentLength = byteArray.Length

而不是 request.ContentType = "application/json"

rather than request.ContentType = "application/json"

这篇关于REST API POST 调用的 (OAuth) 授权请求标头中的不记名令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆