Esempio n. 1
0
        static void Main( string[] args )
        {
            try {
                System.Net.ServicePointManager.Expect100Continue = false;

                OAuthConsumer consumer = new OAuthConsumer( APIKey.ConsumerKey, APIKey.ConsumerSecret );
                consumer.SetTokenWithSecret( APIKey.Token, APIKey.TokenSecret );

                OAuthProvider provider = new OAuthProvider();

                string result = "";

                result = provider.RetrieveRequest( "https://cacoo.com/api/v1/account.xml", consumer );
                Console.WriteLine( "--- アカウント情報取得 ---" );
                Console.WriteLine( result );
                Console.WriteLine( "" );

                result = provider.RetrieveRequest( "https://cacoo.com/api/v1/users/kaorun55.xml", consumer );
                Console.WriteLine( "--- ユーザー情報取得 ---" );
                Console.WriteLine( result );
                Console.WriteLine( "" );

                result = provider.RetrieveRequest( "http://cacoo.com/api/v1/diagrams.xml", consumer );
                Console.WriteLine( "--- 図の一覧取得 ---" );
                Console.WriteLine( result );
                Console.WriteLine( "" );

                result = provider.RetrieveRequest( "http://cacoo.com/api/v1/diagrams/cTedXHIB8T1x1QJS.xml", consumer );
                Console.WriteLine( "--- 図の情報取得 ---" );
                Console.WriteLine( result );
                Console.WriteLine( "" );

                // バイナリの返し方を考える
                //result = provider.RetrieveRequest( "http://cacoo.com/api/v1/diagrams/cTedXHIB8T1x1QJS.png", consumer );
                //Console.WriteLine( "--- 画像取得 ---" );
                //Console.WriteLine( result );
                //Console.WriteLine( "" );
            }
            catch ( Exception ex ) {
                Console.WriteLine( ex.Message );
            }
            finally {
                Console.Write( "Press enter : " );
                Console.ReadLine();
            }
        }
Esempio n. 2
0
        private static void getAccessToken( string consumer_key, string consumer_secret )
        {
            System.Net.ServicePointManager.Expect100Continue = false;
            OAuthBase oAuth = new OAuthBase();

            System.Uri uri = new Uri( OAuth.APIKey.ReqestToken );

            //OAuthBace.csを用いてsignature生成
            OAuth.OAuthConsumer consumer = new OAuthConsumer( consumer_key, consumer_secret );
            string signature = oAuth.GenerateSignature( uri, consumer, "GET", "" );

            //oauth_token,oauth_token_secret取得
            HttpWebRequest webreq = (System.Net.HttpWebRequest)WebRequest.Create( OAuth.APIKey.ReqestToken + string.Format( "?{0}&oauth_signature={1}", oAuth.NormalizedRequestParameters, signature ) );
            webreq.Method = "GET";
            HttpWebResponse webres = (HttpWebResponse)webreq.GetResponse();

            string result;
            using ( System.IO.Stream st = webres.GetResponseStream() )
            using ( System.IO.StreamReader sr = new System.IO.StreamReader( st, Encoding.GetEncoding( 932 ) ) ) {
                result = sr.ReadToEnd();
            }

            Console.WriteLine( result );

            //正規表現でoauth_token,oauth_token_secret取得
            Match match = Regex.Match( result, @"oauth_token=(.*?)&oauth_token_secret=(.*?)&oauth_callback.*" );
            string token = match.Groups[1].Value;
            string tokenSecret = match.Groups[2].Value;

            //ブラウザからPIN確認
            string AuthorizeURL = OAuth.APIKey.Authorize + "?" + result;
            System.Diagnostics.Process.Start( AuthorizeURL );
            Console.Write( "PIN:" );
            string PIN = Console.ReadLine();

            //oauth_token,oauth_token_secretを用いて再びsignature生成
            consumer = new OAuthConsumer( consumer_key, consumer_secret );
            consumer.SetTokenWithSecret( token, tokenSecret );
            signature = oAuth.GenerateSignature( uri, consumer, "POST", "" );
            webreq = (System.Net.HttpWebRequest)WebRequest.Create( OAuth.APIKey.AccessToken + string.Format( "?{3}&oauth_signature={0}&oauth_verifier={2}", signature, result, PIN, oAuth.NormalizedRequestParameters ) );

            //oauth_token,oauth_token_secretの取得
            webreq.Method = "POST";
            webres = (System.Net.HttpWebResponse)webreq.GetResponse();

            using ( System.IO.Stream st = webres.GetResponseStream() )
            using ( System.IO.StreamReader sr = new System.IO.StreamReader( st, Encoding.GetEncoding( 932 ) ) ) {
                result = sr.ReadToEnd();
            }

            Console.WriteLine( result );

            //正規表現でoauth_token,oauth_token_secret取得
            match = Regex.Match( result, @"oauth_token=(.*?)&oauth_token_secret=(.*?)&.*" );
            token = match.Groups[1].Value;
            tokenSecret = match.Groups[2].Value;

            Console.WriteLine( "public const string token = \"" + token + "\";" );
            Console.WriteLine( "public const string tokenSecret = \"" + tokenSecret + "\";" );

            //デスクトップ\oauth_token.txtに保存
            File.WriteAllText( Environment.GetFolderPath( Environment.SpecialFolder.Desktop ) + @"\oauth_token.txt", result );
        }
Esempio n. 3
0
        public void PINを取得後の認証のためのシグニチャ()
        {
            string pin = "0011696";

            OAuth.OAuthBase oauth = new TestingOAuth( "1281614602", "8715791" );

            OAuth.OAuthConsumer consumer = new OAuthConsumer( ConsumerKey, ConsumerSecret );
            consumer.SetTokenWithSecret( "021d4561687d6c5d328ad5b491624f30", "552ae19dc6f2b7736db9678bb4de2f00" );

            Uri uri = new Uri( AccessToken );
            string signature = oauth.GenerateSignature( uri, consumer, "POST", pin );

            Assert.AreEqual( "w9XSZS9loX/pyz6DtO2Q04QmDAw=", signature );
            Assert.AreEqual( "oauth_consumer_key=OKWyYVPvnpcBfbdmrJaNWx&oauth_nonce=8715791&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1281614602&oauth_token=021d4561687d6c5d328ad5b491624f30&oauth_verifier=0011696&oauth_version=1.0",
                oauth.NormalizedRequestParameters );
        }
Esempio n. 4
0
        public void データ取得のためのシグニチャ()
        {
            const string Diagrams = "http://cacoo.com/api/v1/diagrams.xml";

            OAuth.OAuthBase oauth = new TestingOAuth( "1281615317", "2677625" );

            OAuthConsumer consumer = new OAuthConsumer( ConsumerKey, ConsumerSecret );
            consumer.SetTokenWithSecret( "f1b3a9fdb759dbd0c1818f7cdef307c0", "2e55a9ea2e9c286abfedadffecf15f74" );

            Uri uri = new Uri( Diagrams );
            string signature = oauth.GenerateSignature( uri, consumer, "POST", "" );

            Assert.AreEqual( "YRhAaq8P+fN4DgkFhaF6x+EH1qA=", signature );
            Assert.AreEqual( "oauth_consumer_key=\"OKWyYVPvnpcBfbdmrJaNWx\", oauth_nonce=\"2677625\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1281615317\", oauth_token=\"f1b3a9fdb759dbd0c1818f7cdef307c0\", oauth_version=\"1.0\", oauth_signature=\"YRhAaq8P%2BfN4DgkFhaF6x%2BEH1qA%3D\"",
                oauth.AuthorizationRequestParameters );
        }
Esempio n. 5
0
        /// <summary>
        /// アクセストークンの取得
        /// </summary>
        /// <param name="consumer"></param>
        /// <param name="pin"></param>
        public void RetrieveAccessToken( ref OAuthConsumer consumer, string pin )
        {
            // リクエストの作成とレスポンスの取得
            HttpWebRequest webreq = CreateRequest( AccessToken, consumer, POST, pin, RequestFormatAuth );
            string result = GetResponse( webreq );

            // oauth_tokenとoauth_token_secretを取得
            Match match = Regex.Match( result, @"oauth_token=(.*)&oauth_token_secret=(.*)" );
            consumer.SetTokenWithSecret( match.Groups[1].Value, match.Groups[2].Value );
        }
Esempio n. 6
0
        /// <summary>
        /// リクエストトークンを取得する
        /// </summary>
        /// <param name="consumer"></param>
        /// <returns></returns>
        public string RetrieveRequestToken( ref OAuthConsumer consumer )
        {
            // リクエストの作成とレスポンスの取得
            HttpWebRequest webreq = CreateRequest( ReqestToken, consumer, GET, "", RequestFormatAuth );
            string result = GetResponse( webreq );

            // oauth_tokenとoauth_token_secretを取得
            Match match = Regex.Match( result, @"oauth_token=(.*?)&oauth_token_secret=(.*?)&oauth_callback.*" );
            consumer.SetTokenWithSecret( match.Groups[1].Value, match.Groups[2].Value );

            // 認証用URLを返す
            return Authorize + "?" + result;
        }