Bearer token in the (OAuth) Authorization request header for REST API POST call
Hey all i am trying to figure out how to do this OAuth authorization token for a REST API POST call.
The documents state:
Quote:
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"
}
}
Your app can request a new access token by re-running the appropriate flow if this error occurs.
Currently my VB.net code is this:
Code:
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/x-www-form-urlencoded" 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
I keep getting an error of: The remote server returned an error: (401) Unauthorized.
Any help would be great to understand this more!
Re: Yammer REST API POST message to wall/feed
By your description ( and Yammer's ) it looks as if you are on the right track. Disclaimer: I never programmed with Yammer before, but things similar such as Google, Youtube, Facebook.
Does Yammer have any SDKs on their "developer" site? This should have examples that make things easier
Re: Yammer REST API POST message to wall/feed
They dont have anything for VB.net. They only offer javascript, Python and Ruby.
Re: Yammer REST API POST message to wall/feed
Maybe a silly question, but are you 100% that VB is indeed supported?
Re: Yammer REST API POST message to wall/feed
Another thought: Does your computer have a firewall that potentially blocks this?
Re: Yammer REST API POST message to wall/feed
VB is not supported.
Dont know about the firewall but i am already able to get the access token via javascript so i am guessing no its not.
Re: Yammer REST API POST message to wall/feed
This is what i found on stackoverflow abou it:
Quote:
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.
Re: Yammer REST API POST message to wall/feed
I think, message them and ask if VB.NET ( or even the .NET Framework ) is supported. If they say yes, then we'll have to find the reason; if not, we have the reason.
Sorry I couldn't be of more help
Re: Yammer REST API POST message to wall/feed
It can be used because there is a wrapper in .net already made (that i also cant understand how to start)
https://yammersimpleapi.codeplex.com...ammerClient.cs