2016-08-17 14 views
0

twitter APIに問題があります。達成したいのは、ユーザーが自分のWebサイト上でTwitterを使ってログインできることだけです。Twitter APIが401を返す - 権限がありません

私はこのライブラリを使用しています: Twitter

図書館では、GoogleやFacebookに適していますが、ツイッターで、私はそれを動作させることはできません。

私が受けています:

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

は、私はすでに試した何:Twitterの開発者コンソール上

設定アクセストークンと貼り付け、このアクセストークンは、ヘッダします。

私はthis link on twitter からの指示に従い、「承認」ヘッダーを作成しました。それは動作しません、例外はまだ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 v1.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"); 
     }   
    } 
} 

答えて

関連する問題