/// <summary> /// Do the actual upload to Picasa /// </summary> /// <param name="surfaceToUpload">Image to upload</param> /// <param name="outputSettings"></param> /// <param name="title"></param> /// <param name="filename"></param> /// <returns>PicasaResponse</returns> public static string UploadToPicasa(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename) { // Fill the OAuth2Settings OAuth2Settings settings = new OAuth2Settings(); settings.AuthUrlPattern = AuthUrl; settings.TokenUrl = TokenUrl; settings.CloudServiceName = "Picasa"; settings.ClientId = PicasaCredentials.ClientId; settings.ClientSecret = PicasaCredentials.ClientSecret; settings.AuthorizeMode = OAuth2AuthorizeMode.LocalServer; // Copy the settings from the config, which is kept in memory and on the disk settings.RefreshToken = Config.RefreshToken; settings.AccessToken = Config.AccessToken; settings.AccessTokenExpires = Config.AccessTokenExpires; try { var webRequest = OAuth2Helper.CreateOAuth2WebRequest(HTTPMethod.POST, string.Format(UploadUrl, Config.UploadUser, Config.UploadAlbum), settings); if (Config.AddFilename) { webRequest.Headers.Add("Slug", NetworkHelper.EscapeDataString(filename)); } SurfaceContainer container = new SurfaceContainer(surfaceToUpload, outputSettings, filename); container.Upload(webRequest); string response = NetworkHelper.GetResponseAsString(webRequest); return ParseResponse(response); } finally { // Copy the settings back to the config, so they are stored. Config.RefreshToken = settings.RefreshToken; Config.AccessToken = settings.AccessToken; Config.AccessTokenExpires = settings.AccessTokenExpires; Config.IsDirty = true; IniConfig.Save(); } }
public static string UploadToDropbox(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string filename) { OAuthSession oAuth = new OAuthSession(DropBoxCredentials.CONSUMER_KEY, DropBoxCredentials.CONSUMER_SECRET); oAuth.BrowserSize = new Size(1080, 650); oAuth.CheckVerifier = false; oAuth.AccessTokenUrl = "https://api.dropbox.com/1/oauth/access_token"; oAuth.AuthorizeUrl = "https://api.dropbox.com/1/oauth/authorize"; oAuth.RequestTokenUrl = "https://api.dropbox.com/1/oauth/request_token"; oAuth.LoginTitle = "Dropbox authorization"; oAuth.Token = config.DropboxToken; oAuth.TokenSecret = config.DropboxTokenSecret; try { SurfaceContainer imageToUpload = new SurfaceContainer(surfaceToUpload, outputSettings, filename); string uploadResponse = oAuth.MakeOAuthRequest(HTTPMethod.POST, "https://api-content.dropbox.com/1/files_put/sandbox/" + OAuthSession.UrlEncode3986(filename), null, null, imageToUpload); LOG.DebugFormat("Upload response: {0}", uploadResponse); } catch (Exception ex) { LOG.Error("Upload error: ", ex); throw; } finally { if (!string.IsNullOrEmpty(oAuth.Token)) { config.DropboxToken = oAuth.Token; } if (!string.IsNullOrEmpty(oAuth.TokenSecret)) { config.DropboxTokenSecret = oAuth.TokenSecret; } } // Try to get a URL to the uploaded image try { string responseString = oAuth.MakeOAuthRequest(HTTPMethod.GET, "https://api.dropbox.com/1/shares/sandbox/" + OAuthSession.UrlEncode3986(filename), null, null, null); if (responseString != null) { LOG.DebugFormat("Parsing output: {0}", responseString); IDictionary<string, object> returnValues = JSONHelper.JsonDecode(responseString); if (returnValues.ContainsKey("url")) { return returnValues["url"] as string; } } } catch (Exception ex) { LOG.Error("Can't parse response.", ex); } return null; }
private static FacebookInfo UploadToFacebook(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename, bool repeated) { FacebookCredentials oAuth = createSession(true); if (oAuth == null) { return null; } SurfaceContainer container = new SurfaceContainer(surfaceToUpload, outputSettings, filename); var client = new FacebookClient(config.Token); var img = new FacebookMediaObject {FileName = filename}; img.SetValue(container.ToByteArray()); img.ContentType = "multipart/form-data"; var param = new Dictionary<string, object> {{"attachment", img}}; if (!string.IsNullOrEmpty(title)) param.Add("message", title); // Not on timeline? if (config.NotOnTimeline) param.Add("no_story", "1"); JsonObject result; try { result = (JsonObject) client.Post("me/photos", param); } catch (FacebookOAuthException ex) { if (!repeated && ex.ErrorCode == 190) // App no long authorized, reauthorize if possible { config.Token = null; return UploadToFacebook(surfaceToUpload, outputSettings, title, filename, true); } throw; } catch (Exception ex) { LOG.Error("Error uploading to Facebook.", ex); throw; } FacebookInfo facebookInfo = FacebookInfo.FromUploadResponse(result); LOG.Debug("Upload to Facebook was finished"); return facebookInfo; }
/// <summary> /// This will be called when the menu item in the Editor is clicked /// </summary> public string Upload(ICaptureDetails captureDetails, ISurface surfaceToUpload) { SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(_config.UploadFormat, _config.UploadJpegQuality, false); try { string url = null; string filename = Path.GetFileName(FilenameHelper.GetFilename(_config.UploadFormat, captureDetails)); SurfaceContainer imageToUpload = new SurfaceContainer(surfaceToUpload, outputSettings, filename); new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("box", LangKey.communication_wait), delegate { url = BoxUtils.UploadToBox(imageToUpload, captureDetails.Title, filename); } ); if (url != null && _config.AfterUploadLinkToClipBoard) { ClipboardHelper.SetClipboardData(url); } return url; } catch (Exception ex) { LOG.Error("Error uploading.", ex); MessageBox.Show(Language.GetString("box", LangKey.upload_failure) + " " + ex.Message); return null; } }