2013-08-30 3 views
0

Salut à tous, j'essaie de comprendre comment faire ce jeton d'autorisation OAuth pour un appel POST de l'API REST.Jeton de support dans l'en-tête de demande d'autorisation (OAuth) pour l'appel POST de l'API REST

Les documents état:

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

Votre application peut demander un nouveau jeton d'accès en relançant le flux approprié si cette erreur se produit.

Actuellement mon code VB.net est la suivante:

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 

Je continue à obtenir une erreur de: Le serveur distant a renvoyé une erreur: (401) non autorisée.

Je trouve cela dans un Stackoverflow posting suivant:

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.

Toute aide serait bien de comprendre plus!

+0

Comment obtenez-vous 'yammerAPI.userToken'? –

+0

@EugenioPace En passant par chaque état (en utilisant webbrowser). Connexion à yammer, en étant redirigé via mon lien de redirection de l'application qui place le code à la fin de ce ** (http://www.blahblah.com/?code=XYZ) **. Ensuite, je prends ce code et le faire ** Dim url As String = "https://www.yammer.com/oauth2/access_token.json?client_id=" & clientID & "& client_secret =" & clientSecret & "& code =" & authorizedToken ** et analyser le ** JSON ** et obtenir le ** access_token ** de cela. Le ** access_token ** est mon ** yammerAPI.userToken **. – StealthRT

+0

Regardez l'exemple de code dans [cette question] [1]. [1]: http://stackoverflow.com/questions/14188938/net-httpwebrequest-oauth-401-unauthorized –

Répondre

1

Mon expérience récente avec OAuth suggère le type de contenu devrait être:

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

plutôt que request.ContentType = "application/json"

Questions connexes