/// <summary>
        /// Publish to`Twitter
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTwitter_Click(object sender, EventArgs e)
        {
            string           _consumerKey    = "oJs923I3JS4ztSaRpXj5Vg";
            string           _consumerSecret = "h2bG02PmE52BEOlIINdy8M5WKLkDKWBxB0zCH220eQ";
            TwitterService   service         = new TwitterService(_consumerKey, _consumerSecret);
            FormTwitterLogin form            = new FormTwitterLogin(db, service);

            if (form.ShowDialog() == DialogResult.OK)
            {
                FormTwitterPublish publish = new FormTwitterPublish(db, service);
                publish.ShowDialog();
            }
            //FormAuthSocialLogin loginForm = new FormAuthSocialLogin(db, PROVIDER_TYPE.TWITTER, manager);
            //if (loginForm.ShowDialog() == DialogResult.OK)
            //{
            //    FormAuthSocialPublish publishForm = new FormAuthSocialPublish(db, PROVIDER_TYPE.TWITTER, manager);
            //    publishForm.ShowDialog();
            //}
        }
예제 #2
0
        /// <summary>
        /// 媒体连接测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTest_Click(object sender, EventArgs ex)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                string mediaType   = cmbMediaType.Text;
                string mediaUrl    = txtMediaURL.Text;
                string appKey      = txtAppKey.Text;
                string appPassword = txtAppPassword.Text;
                string user        = txtUserName.Text;
                string password    = txtPassword.Text;
                string content     = txtOther.Text;
                string pic         = txtTestImage.Text;
                if (File.Exists(pic))
                {
                    switch (mediaType)
                    {
                    case "TENCENT":
                        FormQWeiboLogin LoginDlg = new FormQWeiboLogin(appKey, appPassword, user, password);
                        LoginDlg.ShowDialog();
                        if (LoginDlg.Comfirm)
                        {
                            OauthKey oauthKey = new OauthKey();
                            oauthKey.customKey    = LoginDlg.AppKey;
                            oauthKey.customSecret = LoginDlg.AppSecret;
                            oauthKey.tokenKey     = LoginDlg.AccessKey;
                            oauthKey.tokenSecret  = LoginDlg.AccessSecret;

                            ///发送带图片微博
                            t            twit = new t(oauthKey, "json");
                            UTF8Encoding utf8 = new UTF8Encoding();

                            string ret = twit.add_pic(utf8.GetString(utf8.GetBytes(content)),
                                                      utf8.GetString(utf8.GetBytes("127.0.0.1")),
                                                      utf8.GetString(utf8.GetBytes("")),
                                                      utf8.GetString(utf8.GetBytes("")),
                                                      utf8.GetString(utf8.GetBytes(pic))
                                                      );

                            string msg = NCMessage.GetInstance(db.Language).GetMessageById("CM0057I", db.Language);
                            MessageBox.Show(msg);
                        }
                        break;

                    case "WORDPRESS":
                        IMetaWeblog          metaWeblog     = (IMetaWeblog)XmlRpcProxyGen.Create(typeof(IMetaWeblog));
                        XmlRpcClientProtocol clientProtocol = (XmlRpcClientProtocol)metaWeblog;
                        clientProtocol.Url = mediaUrl;
                        string picURL   = null;
                        string filename = pic;
                        if (File.Exists(filename))
                        {
                            FileData fileData = default(FileData);
                            fileData.name = Path.GetFileName(filename);
                            fileData.type = Path.GetExtension(filename);
                            try
                            {
                                FileInfo fi = new FileInfo(filename);
                                using (BinaryReader br = new BinaryReader(new FileStream(filename, FileMode.Open, FileAccess.Read)))
                                {
                                    fileData.bits = br.ReadBytes((int)fi.Length);
                                }
                                UrlData urlData = metaWeblog.newMediaObject("6", user, password, fileData);
                                picURL = urlData.url;
                            }
                            catch (Exception exc)
                            {
                                NCLogger.GetInstance().WriteExceptionLog(exc);
                            }
                        }

                        Post newBlogPost = default(Post);
                        newBlogPost.title         = content;
                        newBlogPost.description   = "";
                        newBlogPost.categories    = new string[1];
                        newBlogPost.categories[0] = cmbCategry.Text;
                        newBlogPost.dateCreated   = System.DateTime.Now;
                        if (picURL != null)
                        {
                            newBlogPost.description += "<br><img src='" + picURL + "'/>";
                        }

                        try
                        {
                            string result = metaWeblog.newPost("6", user,
                                                               password, newBlogPost, true);
                        }
                        catch (Exception ex2)
                        {
                            NCLogger.GetInstance().WriteExceptionLog(ex2);
                        }

                        break;

                    case "FACEBOOK":
                        var fbLoginDlg = new FormFacebookLogin(appKey, user, password);
                        fbLoginDlg.ShowDialog();
                        if (fbLoginDlg.FacebookOAuthResult != null && fbLoginDlg.FacebookOAuthResult.IsSuccess)
                        {
                            string _accessToken = fbLoginDlg.FacebookOAuthResult.AccessToken;
                            var    fb           = new FacebookClient(_accessToken);

                            // make sure to add event handler for PostCompleted.
                            fb.PostCompleted += (o, e) =>
                            {
                                // incase you support cancellation, make sure to check
                                // e.Cancelled property first even before checking (e.Error!=null).
                                if (e.Cancelled)
                                {
                                    // for this example, we can ignore as we don't allow this
                                    // example to be cancelled.

                                    // you can check e.Error for reasons behind the cancellation.
                                    var cancellationError = e.Error;
                                }
                                else if (e.Error != null)
                                {
                                    // error occurred
                                    this.BeginInvoke(new MethodInvoker(
                                                         () =>
                                    {
                                        MessageBox.Show(e.Error.Message);
                                    }));
                                }
                                else
                                {
                                    // the request was completed successfully

                                    // make sure to be on the right thread when working with ui.
                                    this.BeginInvoke(new MethodInvoker(
                                                         () =>
                                    {
                                        //MessageBox.Show("Picture uploaded successfully");

                                        Application.DoEvents();
                                    }));
                                }
                            };

                            dynamic parameters = new ExpandoObject();
                            parameters.message = content;
                            parameters.source  = new FacebookMediaObject
                            {
                                ContentType = "image/jpeg",
                                FileName    = Path.GetFileName(pic)
                            }.SetValue(File.ReadAllBytes(pic));

                            fb.PostAsync("me/photos", parameters);
                        }

                        break;

                    case "TWITTER":
                        TwitterService   service = new TwitterService(appKey, appPassword);
                        FormTwitterLogin form    = new FormTwitterLogin(db, service);
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            SendTweetWithMediaOptions sendOptions = new SendTweetWithMediaOptions();
                            sendOptions.Images = new Dictionary <string, Stream>();
                            sendOptions.Images.Add(Path.GetFileName(pic),
                                                   new FileStream(pic, FileMode.Open, FileAccess.Read));
                            if (content.Length > 70)
                            {
                                content = content.Substring(0, 70);
                            }
                            sendOptions.Status = content;

                            if (service.SendTweetWithMedia(sendOptions) != null)
                            {
                                string msg = NCMessage.GetInstance(db.Language).GetMessageById("CM0057I", db.Language);
                                MessageBox.Show(msg);
                            }
                        }
                        break;

                    case "LINKEDIN":
                        OAuth1 _OAuthLinkedin = new OAuth1(db);
                        _OAuthLinkedin.Settings_Provider              = "Linkedin";
                        _OAuthLinkedin.Settings_ConsumerKey           = appKey;
                        _OAuthLinkedin.Settings_ConsumerSecret        = appPassword;
                        _OAuthLinkedin.Settings_AccessToken_page      = "https://api.linkedin.com/uas/oauth/accessToken";
                        _OAuthLinkedin.Settings_Authorize_page        = "https://api.linkedin.com/uas/oauth/authorize";
                        _OAuthLinkedin.Settings_RequestToken_page     = "https://api.linkedin.com/uas/oauth/requestToken";
                        _OAuthLinkedin.Settings_Redirect_URL          = "http://www.chojo.co.jp/";
                        _OAuthLinkedin.Settings_User_agent            = "CJW";
                        _OAuthLinkedin.Settings_OAuth_Realm_page      = "https://api.linkedin.com/";
                        _OAuthLinkedin.Settings_GetProfile_API_page   = "https://api.linkedin.com/v1/people/~/";
                        _OAuthLinkedin.Settings_StatusUpdate_API_page = "https://api.linkedin.com/v1/people/~/current-status";
                        _OAuthLinkedin.getRequestToken();
                        _OAuthLinkedin.authorizeToken();
                        String accessToken = _OAuthLinkedin.getAccessToken();
                        try
                        {
                            string ret = _OAuthLinkedin.APIWebRequest("POST", _OAuthLinkedin.Settings_StatusUpdate_API_page, pic);
                            string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                            xml += "<current-status>" + txtOther.Text + "<img src =" + ret + "/>" + "</current-status>";
                            _OAuthLinkedin.APIWebRequest("PUT", _OAuthLinkedin.Settings_StatusUpdate_API_page, xml);
                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show(exp.Message);
                        }

                        break;

                    case "MSN":
                    case "GOOGLE":
                    case "YAHOO":
                        PROVIDER_TYPE provider_type = (PROVIDER_TYPE)Enum.Parse(typeof(PROVIDER_TYPE), mediaType);
                        setConfigure(user, password, mediaType);
                        FormAuthSocialLogin loginForm = new FormAuthSocialLogin(db, provider_type, socialAuthManager);
                        if (loginForm.ShowDialog() == DialogResult.OK)
                        {
                            string msgs     = HttpUtility.UrlEncode(content);
                            string endpoint = mediaUrl + msgs;

                            string body = String.Empty;
                            //byte[] reqbytes = new ASCIIEncoding().GetBytes(body);
                            byte[] reqbytes = File.ReadAllBytes(pic);
                            Dictionary <string, string> headers = new Dictionary <string, string>();
                            //headers.Add("contentType", "application/x-www-form-urlencoded");
                            headers.Add("contentType", "image/jpeg");
                            headers.Add("FileName", Path.GetFileName(pic));
                            var response = socialAuthManager.ExecuteFeed(
                                endpoint,
                                TRANSPORT_METHOD.POST,
                                provider_type,
                                reqbytes,
                                headers
                                );
                        }
                        break;
                    }
                }
                else
                {
                    string msg = NCMessage.GetInstance(db.Language).GetMessageById("CM0058I", db.Language);
                    MessageBox.Show(msg);
                }
            }
            else
            {
                string msg = NCMessage.GetInstance(db.Language).GetMessageById("CM0050I", db.Language);
                MessageBox.Show(msg);
            }
        }