コード例 #1
0
        //        /// <summary>
        //        /// Submit a web request using OAuth, asynchronously.
        //        /// </summary>
        //        /// <param name="method">GET or POST.</param>
        //        /// <param name="url">The full URL, including the query string.</param>
        //        /// <param name="postData">Data to post (query string format), if POST methods.</param>
        //        /// <param name="callback">The callback to call with the web request data when the asynchronous web request finishes.</param>
        //        /// <returns>The return value of QueueUserWorkItem.</returns>
        //        public bool AsyncWebRequest (RequestMethod method, string url, string postData, Action<string> callback)
        //        {
        //            return ThreadPool.QueueUserWorkItem (new WaitCallback (delegate {
        //                callback (WebRequest (method, url, postData));
        //            }));
        //        }
        /// <summary>
        /// Submit a web request using OAuth.
        /// </summary>
        /// <param name="method">GET or POST.</param>
        /// <param name="url">The full URL, including the query string.</param>
        /// <param name="postData">Data to post (query string format), if POST methods.</param>
        /// <returns>The web server response.</returns>
        private string WebRequest(RequestMethod method, string url, string postData)
        {
            Uri uri = new Uri (url);

            var nonce = GenerateNonce ();
            var timeStamp = GenerateTimeStamp ();

            var outUrl = string.Empty;
            List<IQueryParameter<string>> parameters = null;

            string callbackUrl = string.Empty;
            if (url.StartsWith (apiRoot.OAuthRequestTokenUrl)) {
                callbackUrl = CallbackUrl;
            }

            IOAuthToken token = new OAuthToken { Token = "", Secret = "" };
            // for the access token exchange we need to supply the request token
            if (url.StartsWith (apiRoot.OAuthAccessTokenUrl)) {
                token = this.RequestToken;
                callbackUrl = CallbackUrl;
            } else if (this.AccessToken != null) {
                token = this.AccessToken;
            }
            var sig = GenerateSignature (uri, ConsumerKey, ConsumerSecret, token, verifier, method,
                                         timeStamp, nonce, callbackUrl, out outUrl, out parameters);

            parameters.Add (new QueryParameter<string> ("oauth_signature",
                Uri.EscapeUriString (sig),
                    s => string.IsNullOrEmpty (s))
            );
            parameters.Sort ();

            var ret = MakeWebRequest (method, url, parameters, postData);

            return ret;
        }
コード例 #2
0
		/// <summary>
		/// Syncs the notes.
		/// </summary>
		/// <param name="sender">Sender.</param>
		partial void SyncNotes(NSObject sender) {

			bool success = false;

			if (!String.IsNullOrEmpty (settings.syncURL) || !String.IsNullOrWhiteSpace (settings.syncURL)) {

				var dest_manifest_path = Path.Combine (settings.syncURL, "manifest.xml");
				SyncManifest dest_manifest;
				if (!File.Exists (dest_manifest_path)) {
					using (var output = new FileStream (dest_manifest_path, FileMode.Create)) {
						SyncManifest.Write (new SyncManifest (), output);
					}
				}
				using (var input = new FileStream (dest_manifest_path, FileMode.Open)) {
					dest_manifest = SyncManifest.Read (input);
				}
				var dest_storage = new DiskStorage (settings.syncURL);
				var dest_engine = new Engine (dest_storage);

				var client = new FilesystemSyncClient (NoteEngine, manifestTracker.Manifest);
				var server = new FilesystemSyncServer (dest_engine, dest_manifest);
				new SyncManager(client, server).DoSync ();

				// write back the dest manifest
		        	using (var output = new FileStream (dest_manifest_path, FileMode.Create)) {
					SyncManifest.Write (dest_manifest, output);
				}

				PopulateNotebookList (false);
				RefreshNotesWindowController ();

				success = true;
			}

			else if (!String.IsNullOrEmpty (settings.webSyncURL) ||!String.IsNullOrWhiteSpace (settings.webSyncURL)) {

				ServicePointManager.CertificatePolicy = new DummyCertificateManager();

				OAuthToken reused_token = new OAuthToken { Token = settings.token, Secret = settings.secret };


				ISyncClient client = new FilesystemSyncClient (NoteEngine, manifestTracker.Manifest);
				ISyncServer server = new WebSyncServer (settings.webSyncURL, reused_token);

				new SyncManager (client, server).DoSync ();

				PopulateNotebookList (false);
				RefreshNotesWindowController ();

				success = true;
			}

			if (success) {
				NSAlert alert = new NSAlert () {
					MessageText = "Sync Successful",
					InformativeText = "The sync was successful",
					AlertStyle = NSAlertStyle.Warning
				};
				alert.AddButton ("OK");
				alert.BeginSheet (null);
				alert.Window.Title = "Sync Successful";
			} else {
				NSAlert alert = new NSAlert () {
					MessageText = "Sync Failed",
					InformativeText = "The sync was not successful. Please check the Sync Settings.",
					AlertStyle = NSAlertStyle.Warning
				};
				alert.AddButton ("OK");
				alert.BeginSheet (null);
				alert.Window.Title = "Sync Failed";
			}
        	}
コード例 #3
0
        public bool GetAccessAfterAuthorization(string verifier)
        {
            this.verifier = verifier;
            if (RequestToken == null)
                throw new Exception ("RequestToken");

            var response = Post (apiRoot.OAuthAccessTokenUrl, null, string.Empty);

            if (response.Length == 0) {
                throw new Exception ("received empty response for OAuth authorization");
            }
            //Store the Token and Token Secret
            var qs = System.Web.HttpUtility.ParseQueryString (response);
            AccessToken = new OAuthToken ();
            if (!string.IsNullOrEmpty (qs ["oauth_token"]))
                AccessToken.Token = qs ["oauth_token"];
            if (!string.IsNullOrEmpty (qs ["oauth_token_secret"]))
                AccessToken.Secret = qs ["oauth_token_secret"];
            return true;
        }