コード例 #1
0
ファイル: OAuth.cs プロジェクト: garuma/apachai
        public Task<Tuple<OAuthToken, UserInfos>> AcquireAccessToken(OAuthToken requestToken, string tokenVerifier)
        {
            var headers = new Dictionary<string,string> () {
                { "oauth_consumer_key", config.ConsumerKey },
                { "oauth_nonce", MakeNonce () },
                { "oauth_signature_method", "HMAC-SHA1" },
                { "oauth_timestamp", MakeTimestamp () },
                { "oauth_version", "1.0" }};
            var content = string.Empty;
            headers.Add ("oauth_token", requestToken.Token);
            headers.Add ("oauth_verifier", tokenVerifier);

            string signature = MakeSignature ("POST", AccessUrl, headers);
            string compositeSigningKey = MakeSigningKey (config.ConsumerSecret, requestToken.TokenSecret);
            string oauth_signature = MakeOAuthSignature (compositeSigningKey, signature);

            var wc = new WebClient ();
            headers.Add ("oauth_signature", OAuth.PercentEncode (oauth_signature));

            wc.Headers [HttpRequestHeader.Authorization] = HeadersToOAuth (headers);

            return Task<Tuple<OAuthToken, UserInfos>>.Factory.StartNew (() => {
                    try {
                        ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
                        ServicePointManager.Expect100Continue = false;

                        var result = HttpUtility.ParseQueryString (wc.UploadString (new Uri (AccessUrl), content));

                        OAuthToken token = new OAuthToken (result["oauth_token"], result["oauth_token_secret"]);
                        UserInfos infos = new UserInfos (result["user_id"],
                                                         result["screen_name"]);

                        return Tuple.Create (token, infos);
                    } catch (WebException e) {
                        Console.WriteLine ("Access token error");
                        var x = e.Response.GetResponseStream ();
                        var j = new System.IO.StreamReader (x);
                        Console.WriteLine (j.ReadToEnd ());
                        Console.WriteLine (e);
                        throw e;
                        // fallthrough for errors
                    }
                });
        }
コード例 #2
0
ファイル: OAuth.cs プロジェクト: garuma/apachai
        public string GetAuthorization(OAuthToken tokens, string method, Uri uri, string data)
        {
            var headers = new Dictionary<string, string>() {
                { "oauth_consumer_key", config.ConsumerKey },
                { "oauth_nonce", MakeNonce () },
                { "oauth_signature_method", "HMAC-SHA1" },
                { "oauth_timestamp", MakeTimestamp () },
                { "oauth_token", tokens.Token },
                { "oauth_version", "1.0" }};
            var signatureHeaders = new Dictionary<string,string> (headers);

            // Add the data and URL query string to the copy of the headers for computing the signature
            if (data != null && !string.IsNullOrEmpty (data)){
                var parsed = HttpUtility.ParseQueryString (data);
                foreach (string k in parsed.Keys){
                    signatureHeaders.Add (k, OAuth.PercentEncode (parsed [k]));
                }
            }

            var nvc = HttpUtility.ParseQueryString (uri.Query);
            foreach (string key in nvc){
                if (key != null)
                    signatureHeaders.Add (key, OAuth.PercentEncode (nvc [key]));
            }

            string signature = MakeSignature (method, uri.GetLeftPart (UriPartial.Path), signatureHeaders);
            string compositeSigningKey = MakeSigningKey (config.ConsumerSecret, tokens.TokenSecret);
            string oauth_signature = MakeOAuthSignature (compositeSigningKey, signature);

            headers.Add ("oauth_signature", OAuth.PercentEncode (oauth_signature));

            return HeadersToOAuth (headers);
        }
コード例 #3
0
ファイル: OAuth.cs プロジェクト: garuma/apachai
 public string GetAuthUrl(OAuthToken token)
 {
     return string.Format (AuthenticateUrl, token.Token);
 }
コード例 #4
0
ファイル: OAuth.cs プロジェクト: garuma/apachai
        public Task<OAuthToken> AcquireRequestToken()
        {
            var headers = new Dictionary<string,string> () {
                { "oauth_callback", PercentEncode (config.UrlCallback) },
                { "oauth_consumer_key", config.ConsumerKey },
                { "oauth_nonce", MakeNonce () },
                { "oauth_signature_method", "HMAC-SHA1" },
                { "oauth_timestamp", MakeTimestamp () },
                { "oauth_version", "1.0" }};

            string signature = MakeSignature ("POST", RequestUrl, headers);
            string compositeSigningKey = MakeSigningKey (config.ConsumerSecret, null);
            string oauth_signature = MakeOAuthSignature (compositeSigningKey, signature);

            var wc = new WebClient ();
            headers.Add ("oauth_signature", OAuth.PercentEncode (oauth_signature));
            wc.Headers [HttpRequestHeader.Authorization] = HeadersToOAuth (headers);

            return Task<OAuthToken>.Factory.StartNew (() => {
                    try {
                        ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
                        ServicePointManager.Expect100Continue = false;

                        var result = HttpUtility.ParseQueryString (wc.UploadString (new Uri (RequestUrl), string.Empty));
                        OAuthToken token = new OAuthToken (result["oauth_token"], result["oauth_token_secret"]);
                        return token;
                    } catch (WebException e) {
                        Console.WriteLine ("Request token error");
                        Console.WriteLine (e.ToString ());
                        var x = e.Response.GetResponseStream ();
                        var j = new System.IO.StreamReader (x);
                        Console.WriteLine (j.ReadToEnd ());
                        throw e;
                    }
                });
        }