2013-08-30 4 views
0

안녕하세요, 저는 REST API POST 호출을 위해이 OAuth 인증 토큰을 수행하는 방법을 알아 내려고하고 있습니다.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 코드는 이것이다 : (401) 권한이 없음 : 원격 서버에서 오류를 반환 :

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 

나는의 오류가 계속.

은 내가 다음 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 각 상태 (웹 브라우저 사용)로 이동합니다. 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"