0

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

文件指出:

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"
  }
}

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

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

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 帖子中发现了这一点:

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

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

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 数据在 Authorization 标头中,而不是在 URL 中。唯一在 URL 中有任何 OAuth 数据的时间是在您进行授权时。

任何帮助都会很好地理解这一点!

4

1 回答 1

1

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

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

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

于 2015-11-25T05:16:40.857 回答