Пример #1
0
        /// <summary>
        ///     Do the actual upload to OneDrive
        /// </summary>
        /// <param name="oAuth2Settings">OAuth2Settings</param>
        /// <param name="surfaceToUpload">ISurface to upload</param>
        /// <param name="outputSettings">OutputSettings for the image file format</param>
        /// <param name="title">Title</param>
        /// <param name="filename">Filename</param>
        /// <param name="progress">IProgress</param>
        /// <param name="token">CancellationToken</param>
        /// <returns>ImgurInfo with details</returns>
        public static async Task <OneDriveUploadResponse> UploadToOneDriveAsync(OAuth2Settings oAuth2Settings, ISurface surfaceToUpload,
                                                                                SurfaceOutputSettings outputSettings, string title, string filename, IProgress <int> progress = null,
                                                                                CancellationToken token = default)
        {
            var uploadUri      = new Uri(UrlUtils.GetUploadUrl(filename));
            var localBehaviour = Behaviour.ShallowClone();

            if (progress != null)
            {
                localBehaviour.UploadProgress = percent => { UiContext.RunOn(() => progress.Report((int)(percent * 100)), token); };
            }
            var oauthHttpBehaviour = OAuth2HttpBehaviourFactory.Create(oAuth2Settings, localBehaviour);

            using (var imageStream = new MemoryStream())
            {
                ImageOutput.SaveToStream(surfaceToUpload, imageStream, outputSettings);
                imageStream.Position = 0;
                using (var content = new StreamContent(imageStream))
                {
                    content.Headers.Add("Content-Type", "image/" + outputSettings.Format);
                    oauthHttpBehaviour.MakeCurrent();
                    return(await uploadUri.PostAsync <OneDriveUploadResponse>(content, token));
                }
            }
        }
Пример #2
0
 public OneDriveDestination(IOneDriveConfiguration config, IOneDriveLanguage oneDriveLanguage)
 {
     _resources        = new ComponentResourceManager(typeof(OneDriveDestination));
     _config           = config;
     _oneDriveLanguage = oneDriveLanguage;
     // Configure the OAuth2 settings for OneDrive communication
     _oauth2Settings = new Dapplo.HttpExtensions.OAuth.OAuth2Settings
     {
         AuthorizationUri = new Uri("login.microsoftonline.com/common/oauth2/v2.0/authorize")
                            .ExtendQuery(new Dictionary <string, string>
         {
             { "response_type", "code" },
             { "client_id", "{ClientId}" },
             { "redirect_uri", "{RedirectUrl}" },
             { "state", "{State}" },
             { "scope", "files.readwrite offline_access" }
         }),
         TokenUrl         = new Uri("https://login.microsoftonline.com/common/oauth2/v2.0/token"),
         CloudServiceName = "OneDrive",
         ClientId         = OneDriveCredentials.CLIENT_ID,
         ClientSecret     = OneDriveCredentials.CLIENT_SECRET,
         RedirectUrl      = "http://getgreenshot.org",
         AuthorizeMode    = AuthorizeModes.EmbeddedBrowser,
         Token            = config
     };
 }
Пример #3
0
        /// <summary>
        ///     Do the actual upload to Imgur
        ///     For more details on the available parameters, see: http://api.imgur.com/resources_anon
        /// </summary>
        /// <param name="oAuth2Settings">OAuth2Settings</param>
        /// <param name="surfaceToUpload">ISurface to upload</param>
        /// <param name="outputSettings">OutputSettings for the image file format</param>
        /// <param name="title">Title</param>
        /// <param name="filename">Filename</param>
        /// <param name="progress">IProgress</param>
        /// <param name="token">CancellationToken</param>
        /// <returns>ImgurInfo with details</returns>
        public static async Task <ImgurImage> UploadToImgurAsync(OAuth2Settings oAuth2Settings, ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename, IProgress <int> progress = null, CancellationToken token = default)
        {
            IDictionary <string, string> otherParameters = new Dictionary <string, string>();

            // add title
            if ((title != null) && _imgurConfiguration.AddTitle)
            {
                otherParameters.Add("title", title);
            }
            // add filename
            if ((filename != null) && _imgurConfiguration.AddFilename)
            {
                otherParameters.Add("name", filename);
            }
            ImgurImage imgurImage;

            if (_imgurConfiguration.AnonymousAccess)
            {
                imgurImage = await AnnonymousUploadToImgurAsync(surfaceToUpload, outputSettings, otherParameters, progress, token).ConfigureAwait(false);
            }
            else
            {
                imgurImage = await AuthenticatedUploadToImgurAsync(oAuth2Settings, surfaceToUpload, outputSettings, otherParameters, progress, token);
            }
            return(imgurImage);
        }
Пример #4
0
		public OAuth2Tests(ITestOutputHelper testOutputHelper)
		{
			LogSettings.RegisterDefaultLogger<XUnitLogger>(LogLevels.Verbose, testOutputHelper);
			var oAuth2Settings = new OAuth2Settings
			{
				ClientId = "<client id from google developer console>",
				ClientSecret = "<client secret from google developer console>",
				CloudServiceName = "Google",
				AuthorizeMode = AuthorizeModes.LocalhostServer,
				TokenUrl = GoogleApiUri.AppendSegments("oauth2", "v4", "token"),
				AuthorizationUri = new Uri("https://accounts.google.com").AppendSegments("o", "oauth2", "v2", "auth").ExtendQuery(new Dictionary<string, string>
				{
					{"response_type", "code"},
					{"client_id", "{ClientId}"},
					{"redirect_uri", "{RedirectUrl}"},
					{"state", "{State}"},
					{"scope", GoogleApiUri.AppendSegments("auth", "calendar").AbsoluteUri}
				})
			};
			_oAuthHttpBehaviour = OAuth2HttpBehaviourFactory.Create(oAuth2Settings);
		}
		/// <summary>
		///     Create a HttpMessageHandler which handles the OAuth 2 communication for you
		/// </summary>
		/// <param name="oAuth2Settings">OAuth2Settings</param>
		/// <param name="httpBehaviour">IHttpBehaviour</param>
		/// <param name="innerHandler">HttpMessageHandler</param>
		public OAuth2HttpMessageHandler(OAuth2Settings oAuth2Settings, IHttpBehaviour httpBehaviour, HttpMessageHandler innerHandler) : base(innerHandler)
		{
			if (oAuth2Settings.ClientId == null)
			{
				throw new ArgumentNullException(nameof(oAuth2Settings.ClientId));
			}
			if (oAuth2Settings.ClientSecret == null)
			{
				throw new ArgumentNullException(nameof(oAuth2Settings.ClientSecret));
			}
			if (oAuth2Settings.TokenUrl == null)
			{
				throw new ArgumentNullException(nameof(oAuth2Settings.TokenUrl));
			}

			_oAuth2Settings = oAuth2Settings;
			var newHttpBehaviour = httpBehaviour.ShallowClone();
			// Remove the OnHttpMessageHandlerCreated
			newHttpBehaviour.OnHttpMessageHandlerCreated = null;
			// Use it for internal communication
			_httpBehaviour = newHttpBehaviour;
		}
Пример #6
0
        /// <summary>
        ///     Create a HttpMessageHandler which handles the OAuth 2 communication for you
        /// </summary>
        /// <param name="oAuth2Settings">OAuth2Settings</param>
        /// <param name="httpBehaviour">IHttpBehaviour</param>
        /// <param name="innerHandler">HttpMessageHandler</param>
        public OAuth2HttpMessageHandler(OAuth2Settings oAuth2Settings, IHttpBehaviour httpBehaviour, HttpMessageHandler innerHandler) : base(innerHandler)
        {
            if (oAuth2Settings.ClientId is null)
            {
                throw new ArgumentNullException(nameof(oAuth2Settings.ClientId));
            }
            if (oAuth2Settings.ClientSecret is null)
            {
                throw new ArgumentNullException(nameof(oAuth2Settings.ClientSecret));
            }
            if (oAuth2Settings.TokenUrl is null)
            {
                throw new ArgumentNullException(nameof(oAuth2Settings.TokenUrl));
            }

            _oAuth2Settings = oAuth2Settings;
            var newHttpBehaviour = httpBehaviour.ShallowClone();

            // Remove the OnHttpMessageHandlerCreated
            newHttpBehaviour.OnHttpMessageHandlerCreated = null;
            // Use it for internal communication
            _httpBehaviour = newHttpBehaviour;
        }
Пример #7
0
        /// <summary>
        ///     Do the actual upload to Picasa
        /// </summary>
        /// <param name="oAuth2Settings">OAuth2Settings</param>
        /// <param name="surfaceToUpload">ICapture</param>
        /// <param name="outputSettings">SurfaceOutputSettings</param>
        /// <param name="otherParameters">IDictionary</param>
        /// <param name="progress">IProgress</param>
        /// <param name="token"></param>
        /// <returns>PicasaResponse</returns>
        public static async Task <ImgurImage> AuthenticatedUploadToImgurAsync(OAuth2Settings oAuth2Settings, ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, IDictionary <string, string> otherParameters, IProgress <int> progress = null, CancellationToken token = default)
        {
            var uploadUri      = new Uri(_imgurConfiguration.ApiUrl).AppendSegments("upload.json").ExtendQuery(otherParameters);
            var localBehaviour = Behaviour.ShallowClone();

            if (progress != null)
            {
                localBehaviour.UploadProgress = percent => { UiContext.RunOn(() => progress.Report((int)(percent * 100)), token); };
            }
            var oauthHttpBehaviour = OAuth2HttpBehaviourFactory.Create(oAuth2Settings, localBehaviour);

            using (var imageStream = new MemoryStream())
            {
                ImageOutput.SaveToStream(surfaceToUpload, imageStream, outputSettings);
                imageStream.Position = 0;
                using (var content = new StreamContent(imageStream))
                {
                    content.Headers.Add("Content-Type", "image/" + outputSettings.Format);
                    oauthHttpBehaviour.MakeCurrent();
                    return(await uploadUri.PostAsync <ImgurImage>(content, token));
                }
            }
        }
Пример #8
0
 public ImgurDestination(IImgurConfiguration imgurConfiguration, IImgurLanguage imgurLanguage)
 {
     _imgurConfiguration = imgurConfiguration;
     _imgurLanguage      = imgurLanguage;
     // Configure the OAuth2 settings for Imgur communication
     _oauth2Settings = new Dapplo.HttpExtensions.OAuth.OAuth2Settings
     {
         AuthorizationUri = new Uri("https://api.imgur.com").AppendSegments("oauth2", "authorize").
                            ExtendQuery(new Dictionary <string, string>
         {
             { "response_type", "code" },
             { "client_id", "{ClientId}" },
             { "redirect_uri", "{RedirectUrl}" },
             { "state", "{State}" }
         }),
         TokenUrl         = new Uri("https://api.imgur.com/oauth2/token"),
         CloudServiceName = "Imgur",
         ClientId         = imgurConfiguration.ClientId,
         ClientSecret     = imgurConfiguration.ClientSecret,
         RedirectUrl      = "http://getgreenshot.org",
         AuthorizeMode    = AuthorizeModes.EmbeddedBrowser,
         Token            = imgurConfiguration
     };
 }