嘿所有我想弄清楚如何为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 posting中找到了这个:
Yammer API要求OAuth数据位于标头中。如果您查看他们获取数据的示例,您会看到请求看起来像。
GET / api / v1 / messages / favorites_of / 1234 HTTP / 1.1 主持人: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”< / p>
OAuth数据位于Authorization标头中,而不在URL中。您在URL中有任何OAuth数据的唯一时间就是进行授权。
任何帮助都会很好理解这一点!
答案 0 :(得分:1)
我最近与Oauth的经历表明内容类型应为:
Request.ContentType = "application/x-www-form-urlencoded"
Request.Method = "POST"
Request.ContentLength = byteArray.Length
而不是request.ContentType =&#34; application / json&#34;