2013-08-30 97 views
0

嘿,我想知道如何爲REST API POST調用執行此OAuth授權令牌。REST API的(OAuth)授權請求標頭中的不記名令牌POST調用

文件的狀態:

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

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 HOST: 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"

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.

任何幫助將是巨大的,明白這一點更多!

+0

你是如何獲得'yammerAPI.userToken'? –

+0

@EugenioPace通過瀏覽每個狀態(使用webbrowser)。登錄yammer,通過應用程序的重定向鏈接重定向,該鏈接將代碼放在**(http://www.blahblah.com/?code=XYZ)**末尾。然後,我採取該代碼,並執行此操作** Dim url As String =「https://www.yammer.com/oauth2/access_token.json?client_id=」&clientID&「&client_secret =」&clientSecret&「&code =」& authorizedToken **並解析** JSON **並從中獲取** access_token **。 ** access_token **是我的** yammerAPI.userToken **。 – StealthRT

+0

看看[這個問題] [1]中的代碼示例。 [1]:http://stackoverflow.com/questions/14188938/net-httpwebrequest-oauth-401-unauthorized –

回答

1

我最近使用OAuth的經驗表明內容類型應該是:

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

而非request.ContentType = 「應用/ JSON」