[英]Bearer token in the (OAuth) Authorization request header for REST API POST call
嘿,我正在嘗試找出如何為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
授權: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數據的時間是您進行授權的時間。
任何幫助都將有助於您更好地理解這一點!
我最近對Oauth的經驗表明,內容類型應為:
Request.ContentType = "application/x-www-form-urlencoded" Request.Method = "POST" Request.ContentLength = byteArray.Length
而不是request.ContentType =“ application / json”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.