2015-01-21 5 views
0

나는 https://developer.yahoo.com/oauth2/guide/에서 야후의 가이드에 따라 Yahoo OAuth 2.0을 구현 중이다. 내 코드는 "액세스 토큰에 대한 Exchange 인증 코드"라는 가이드의 4 단계에서 실패합니다. "원격 서버가 오류를 반환했습니다 : (400) Bad Request."오류가 발생했습니다. 내 앱이 실시간으로 오류를 볼 수있는 http://www.schoonheidsinstituut-antwerpen.com/yahooapi.aspx에 있습니다.원격 서버에서 오류를 반환했습니다 : (400) 잘못된 요청입니다. in yahoo Oath 2.0

내 C# 코드는 아래에 주어진다 -

string consumerKey = "mykey1"; 
string consumerSecret = "mykey2"; 
string returnUrl = "http://www.schoonheidsinstituut-antwerpen.com/yahooapi.aspx"; 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (Request.QueryString["code"] != null) 
     GetAccessToken(); 
} 
protected void yahooButton_Click(object sender, EventArgs e) 
{ 
    /*Sending User To Authorize Access Page*/ 
    string url = "https://api.login.yahoo.com/oauth2/request_auth?client_id=" + consumerKey + "&redirect_uri=" + returnUrl + "&response_type=code&language=en-us"; 
    Response.Redirect(url); 
    /*End*/ 
} 
public void GetAccessToken() 
{ 
    /*Exchange authorization code for Access Token by sending Post Request*/ 
    Uri address = new Uri("https://api.login.yahoo.com/oauth2/get_token"); 

    // Create the web request 
    HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; 

    // Set type to POST 
    request.Method = "POST"; 
    request.ContentType = "application/x-www-form-urlencoded"; 
    request.Headers["Authorization"] = "Basic"; 

    // Create the data we want to send 
    StringBuilder data = new StringBuilder(); 
    data.Append("client_id=" + consumerKey); 
    data.Append("&client_secret=" + consumerSecret); 
    data.Append("&redirect_uri=" + returnUrl); 
    data.Append("&code=" + Request.QueryString["code"]); 
    data.Append("&grant_type=authorization_code"); 

    // Create a byte array of the data we want to send 
    byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); 

    // Set the content length in the request headers 
    request.ContentLength = byteData.Length; 

    // Write data 
    using (Stream postStream = request.GetRequestStream()) 
    { 
     postStream.Write(byteData, 0, byteData.Length); 
    } 

    // Get response 
    string responseFromServer = ""; 
    try 
    { 
     using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
     { 
      // Get the response stream 
      StreamReader reader = new StreamReader(response.GetResponseStream()); 
      responseFromServer = reader.ReadToEnd(); 
     } 
    } 
    catch (Exception ex) 
    { 
     dataDiv.InnerHtml = ex.Message; 
    } 
    /*End*/   
} 

페이지 HTML 것은 -

<form id="form1" runat="server"> 
    <div> 
     <asp:Button ID="yahooButton" Text="Get Yahoo Contact" runat="server" OnClick="yahooButton_Click" /> 
    </div> 
    <div id="dataDiv" runat="server"></div> 
</form> 

은 내가 HTTP POST 요청이 400 오류 코드와 함께 실패는 사실을 알게 될 것입니다. 누군가이 오류의 원인을 찾는데 도와 주실 수 있습니까? 덕분에 .

답변

1

답변을 찾는 데 거의 1 주일을 보냅니다. 나는 마침내 오류를주고있는 것을 얻었다. "Note : Authorization : 기본 인증 헤더는 Base64 인코딩 인 RFC : 2617에 따라 client_id : client_secret를 통해 생성됩니다."라는 야후 문서에서 노트를 구현하는 것을 잊었습니다.

byte[] headerByte=System.Text.Encoding.UTF8.GetBytes(consumerKey+":"+consumerSecret); 
    string headerString=System.Convert.ToBase64String(headerByte); 
    request.Headers["Authorization"] = "Basic " + headerString; 

지금은 잘 작동하고 내가 야후에서 access_token은 무엇입니까를 -

나는있는 코드의 내 HTTP 포스트 헤더 부분에 두 줄의 코드를 추가했습니다.

도움이 필요한 사람에게 도움이되기를 바랍니다.

감사

요기