2016-08-17 4 views
0

트위터 API에 문제가 있습니다. 내가 성취하고자하는 것은 사용자가 내 웹 사이트에서 트위터를 통해 로그인 할 수 있다는 것입니다. Twitter트위터 API가 401을 반환합니다. - 승인되지 않았습니다.

도서관은 구글과 페이스 북에서 잘 작동하지만, 트위터와 나는 그것이 작동 할 수 없습니다 :

나는이 라이브러리를 사용하고 있습니다.

내가 받고 오전 :

System.Net.WebException: The remote server returned an error: (401) Unauthorized.

가 이미 시도했습니다 무엇 트위터 개발자 콘솔에

설정 액세스 토큰 및 붙여 넣기이 액세스 토큰이 헤더 할 수 있습니다.

나는 this link on twitter 의 지침을 따라 'Authorization'헤더를 만들었습니다. 그것은 작동하지 않습니다, 예외는 여전히 401 - 승인되지 않습니다.

내가 이해할 수없는 한 가지는 this link입니다.

401 오류에 대한 설명은 다음과 같습니다

Authentication credentials were missing or incorrect. Also returned in other circumstances, for example all calls to API v1 endpoints now return 401 (use API v1.1 instead).

그래서, API v1에 대한 모든 통화 '대신 사용 API 버전 1.1'(401)를 반환합니다 -이 어떻게 할까? v1 대신 v1.1을 사용하는 설정은 어디에 있습니까? 여기

내 수정하여, 원래의 코드에서 방법입니다 - 추가 '인증'헤더 :

namespace Oauth2Login.Core 
{ 
    public class RestfullRequest 
    { 
     public static string Request(string url, string method, string contentType, NameValueCollection authorizationHeader, 
      string data, string proxyAddress = null) 
     { 
      var request = (HttpWebRequest) WebRequest.Create(url); 
      request.Method = method; 
      if (!string.IsNullOrEmpty(contentType)) 
       request.ContentType = contentType; 

      string authHeaderString = "OAuth "; 
      authHeaderString += PercentEncode("oauth_consumer_key") + "=\"" + (authorizationHeader.GetValues("oauth_consumer_key"))[0] + "\", "; 
      authHeaderString += PercentEncode("oauth_nonce") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_nonce"))[0]) + "\", "; 
      authHeaderString += PercentEncode("oauth_signature") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_signature"))[0]) + "\", "; 
      authHeaderString += PercentEncode("oauth_signature_method") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_signature_method"))[0]) + "\", "; 
      authHeaderString += PercentEncode("oauth_timestamp") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_timestamp"))[0]) + "\", "; 
      authHeaderString += PercentEncode("oauth_token") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_token"))[0]) + "\", "; 
      authHeaderString += PercentEncode("oauth_version") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_version"))[0]) + "\""; 

      request.Headers.Add("Authorization", authHeaderString); 

      if (!string.IsNullOrEmpty(proxyAddress)) 
      { 
       IWebProxy proxy = new WebProxy(proxyAddress); 
       proxy.Credentials = new NetworkCredential(); 
       request.Proxy = proxy; 
      } 

      if (!string.IsNullOrEmpty(data)) 
      { 
       using (var swt = new StreamWriter(request.GetRequestStream())) 
       { 
        swt.Write(data); 
       } 
      } 

      string result = string.Empty; 

      /*this line throws 401 - not authorized exception*/ 
      using (WebResponse response = request.GetResponse()) 
      { 
       using (var sr = new StreamReader(response.GetResponseStream())) 
       { 
        result = sr.ReadToEnd(); 
       } 
      } 
      return result; 
     } 

     public static string PercentEncode(string source) 
     { 
      return source 
       .Replace(" ", "%20").Replace("!", "%21").Replace("&", "%26") 
       .Replace(@"/", "%2F").Replace("=", "%3D").Replace("+", "%2B") 
       .Replace(",", "%2C").Replace("-", "%2D").Replace(".", "%2E"); 
     }   
    } 
} 

답변

관련 문제