public static async Task <RequestResult> SendTweet(string text, List <string> media_ids)
        {
            var parameters = new HttpParameterCollection();

            parameters.AddFormParameter("status", text);

            // images
            if (media_ids != null && media_ids.Count > 0)
            {
                parameters.AddFormParameter("media_ids", String.Join(",", media_ids));
            }

            var t = Task.Run <RequestResult>(() =>
            {
                return(OAuthUtility.Post
                       (
                           "https://api.twitter.com/1.1/statuses/update.json",
                           parameters: parameters,
                           authorization: TwitterApi.GetAuth(),
                           contentType: "application/x-www-form-urlencoded"
                       ));
            });

            return(await t);
        }
Beispiel #2
0
        /// <summary>
        /// Sends a request to refresh the access token.
        /// </summary>
        /// <param name="accessToken">May contain an access token, which should be refreshed.</param>
        /// <remarks>
        /// <para>If <paramref name="accessToken"/> parameter is not specified, it will use the current access token from the same property of the current class instance.</para>
        /// <para>Token must contain the <b>refresh_token</b>, which was received together with the access token.</para>
        /// </remarks>
        /// <exception cref="NotSupportedException">
        /// <para>Provider does not support refreshing the access token, or the method is not implemented.</para>
        /// <para>Use the property <see cref="OAuthBase.SupportRefreshToken"/>, to check the possibility of calling this method.</para>
        /// </exception>
        /// <exception cref="AccessTokenException">
        /// <para>Access token is not found or is not specified.</para>
        /// <para>-or-</para>
        /// <para><b>refresh_token</b> value is empty.</para>
        /// </exception>
        /// <exception cref="RequestException">Error during execution of a web request.</exception>
        public override AccessToken RefreshToken(AccessToken accessToken = null)
        {
            if (!this.SupportRefreshToken)
            {
                throw new NotSupportedException();
            }

            var token = (OAuth2AccessToken)base.GetSpecifiedTokenOrCurrent(accessToken, refreshTokenRequired: true);

            var parameters = new HttpParameterCollection();

            parameters.AddFormParameter("access_token", token.Value);
            parameters.AddFormParameter("client_id", this.ApplicationId);
            parameters.AddFormParameter("client_secret", this.ApplicationSecret);
            parameters.AddFormParameter("grant_type", GrantType.RefreshToken);
            parameters.AddFormParameter("refresh_token", token.RefreshToken);

            var result = OAuthUtility.Post
                         (
                this.AccessTokenUrl,
                parameters: parameters
                         );

            return(new OAuth2AccessToken(result));
        }
Beispiel #3
0
        public string GetTumbNail(string id)
        {
            var parameter = new HttpParameterCollection()
            {
                { "access_token", driveinfo.token.access_token }
            };
            var result = OAuthUtility.Get(string.Format("https://api.onedrive.com/v1.0/drive/items/{0}/thumbnails", id), parameter);
            var map    = new ApiDataMapping();

            map.Add("medium", "Thumnail");
            FileInfo fi = null;

            foreach (var item in result.Result.CollectionItems.Items["value"].CollectionItems.Items.Values)
            {
                fi = new FileInfo(item, map);
            }
            if (fi != null)
            {
                int index = fi.Thumnail.IndexOf("url");
                int last  = fi.Thumnail.LastIndexOf(",");
                fi.Thumnail = fi.Thumnail.Substring(index, last - index);
                fi.Thumnail = fi.Thumnail.Substring(7, (fi.Thumnail.Length - 3) - 5);
                return(fi.Thumnail);
            }
            else
            {
                return(null);
            }
        }
Beispiel #4
0
        public void ExecuteRequestTest()
        {
            var parameters = new HttpParameterCollection
            {
                { "content", "hello world!" },
                { "contentType", "text/plain" }
            };

            var result = OAuthUtility.Get("http://api.foxtools.ru/v2/Http", parameters);

            Assert.True(result.Contains("hello world!"));

            parameters = new HttpParameterCollection
            {
                { Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAFUAAABVCAYAAAA49ahaAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAAR/SURBVHhe7ZDRbiMxDAPv/3+6px62gDomzGVsb+4hA0wNiJSS5k/x9aSO3X3mp70+U4endOzuMz/t9Zk6PKVjd5/5aa/P1OEpHbv7zE97feY43Im7n+ZO4vJV1P16x+FO3P00dxKXr6Lu1zsOd+Lup7mTuHwVdb/ecbgTdz/NncTlq6j79Y7DDnMnSXNKVKdL0txJVF7vOOwwd5I0p0R1uiTNnUTl9Y7DDnMnSXNKVKdL0txJVF7vOOwwd5I0p0R1uiTNnUTl9Y7DDnMnSfNUh+szdxKV1zsOO8ydJM1THa7P3ElUXu847DB3kjRPdbg+cydReb3jsMPcSdI81eH6zJ1E5fWOww5zJ3E5cX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uQ+3MvEP19PIpSZrvlqzm39T8d+m0JM13S1bzb2r+u3Rakua7Jav5NzX/XTotSfPdktX8Gz39sMDX11/4Q2CGtGAk5wAAAABJRU5ErkJggg==") },
                { "test", 123 },
                { new HttpFormParameter("x", "y") },
                { new HttpFormParameter("a", "bc") },
                { "contentType", "text/plain" }
            };

            result = OAuthUtility.Post("http://api.foxtools.ru/v2/Http", parameters);

            Assert.True(result.ToString().IndexOf("PNG") != -1);

            parameters = new HttpParameterCollection
            {
                { new HttpFile("file1", "test.png", "image/png", Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAEAAAAA/CAYAAABQHc7KAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDoAABSCAABFVgAADqXAAAXb9daH5AAACLOSURBVHja5Jp3VNTXtsd/gBRFEJA2wNBhYBgcYGboQxuGNggIIlUEISgan8ZrucbEFpNAosaSGEuaqBRJ0dhNTIiioMbevVFjiSWWaK6x6+f98/glXPG1de99d6131vouBmbWcPbn7L3PPvv8BED4/ywBEAYPHvxcDRkyRKiurhYmTJggvPbaa8Ls2bOFRYsWCU1NTcKOHTuE/fv3C+fOnRNu374tnDt3Tti/f7/Q2toqtLS0CJ988okwd+5cy9deey1s4sSJhTU1Na+WlZUtKSgo+DorK+u0Xq+/rtVqL2k0mn3BwcFNfn5+s/z8/EZ4eHjobWxs3GxsbIx69+4tmJqaCqampoKRkZHwvGFsbCz07t1bMDY2Fn+3s7MTnJ2dBScnp271DwGwb98+obW11e2zzz4rr6+vb1iwYMHp2bNnP5w9ezbz589n6dKl1NfX09jYSFNTE8uXL2fx4sXU1tYyZswYcnNzUavVTx0dHX/p1avXdhMTkxmmpqYxpqampv/qAHpcuHBBf/jw4Yb29vabra2tbNu2jf3793P69GmuXbvGnTt3uH//Pk+ePOF54+HDh1y+fJm2tjbmzJlDWloatra2TwRB2C8IwjhBEFz/1QCYnDt3LvuXX37ZfuPGDc6dO8e5c+e4ceMGv/76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l7+pLi4eH9WVta4jIwMV71eL0RGRgparfZfB8BXX30lbNy4Ufj666+F9vZ2t7a2tvJTp041nD9//vTly5cfnjp1itbWVpqbm1m4cCG1tbVMmTKFP//5z4wfP56JEycyadIkXn75ZSZPnvx0woQJv4wdO3Z7dXX1jNLS0pjCwkLTzMxMISMjQ/iXBrB161Zh586dwubNm4WDBw8KBw4csDx16lTY+fPnC3/44YdXjx07tmTPnj1ff/vtt2fWrl17vbGx8fLSpUv3zZ8/v+mtt96aNXPmzBGvvPKKfty4cdLRo0cbDR06VCgqKhKKioqEfxiA/8/69wEAGjgk0I9Qd/wAAAAASUVORK5CYII=")) },
                { "dd", 20 },
                { "id", 13 },
                { new HttpFormParameter("key", "e91268b7-1162-4044-9236-7191c8c3b5d2") },
                { "contentType", "text/plain" }
            };

            result = OAuthUtility.Post("http://api.foxtools.ru/v2/Http", parameters);

            Assert.True(result.ToString().Contains("test.png"));
            Assert.True(result.ToString().Contains("e91268b7-1162-4044-9236-7191c8c3b5d2"));

            using (var fs = new FileStream(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "oauth.dat"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                parameters = new HttpParameterCollection
                {
                    { new HttpFile("file1", "123.png", "image/png", fs) },
                    { "dd", 20 },
                    { "id", 13 },
                    { "key", "e91268b7-1162-4044-9236-7191c8c3b5d2" },
                    { "contentType", "text/plain" }
                };

                result = OAuthUtility.ExecuteRequest("POST", "http://api.foxtools.ru/v2/Http", parameters);

                Assert.True(result.ToString().Contains("123.png"));
            }

            result = OAuthUtility.Get("https://github.com/alekseynemiro/nemiro.oauth.dll/archive/master.zip");

            Assert.True(result.IsFile);
            Assert.Contains("master.zip", result.FileName);
        }
Beispiel #5
0
        private async void AllSerach()
        {
            foreach (var item in Setting.driveinfo)
            {
                CloudFiles files = null;
                if (item.token.Drive == "Google")
                {
                    string query     = "title = " + "'" + serchname + "'";
                    var    parameter = new Dictionary <string, string>
                    {
                        { "q", query }
                    };

                    var result = await HttpHelper.RequstHttp("GET", "https://www.googleapis.com/drive/v2/files", parameter, item.token.access_token);

                    Dictionary <string, object> fileinfo = HttpHelper.DerealizeJson(result.GetResponseStream());
                    object[] items = (object[])fileinfo["items"];
                    googlecloud1.Folder.GoogleFolder folder = new Folder.GoogleFolder(item);
                    foreach (var fi in items)
                    {
                        files = folder.AddFiles((Dictionary <string, object>)fi);
                        file.Add(files);
                    }
                }
                else if (item.token.Drive == "OneDrive")
                {
                    var parameter = new Dictionary <string, string>
                    {
                        { "q", serchname }
                    };
                    var result = await HttpHelper.RequstHttp("GET", "https://api.onedrive.com/v1.0/drive/root/view.search", parameter, item.token.access_token);

                    Dictionary <string, object> fileinfo = HttpHelper.DerealizeJson(result.GetResponseStream());
                    object[] items = (object[])fileinfo["value"];
                    googlecloud1.Folder.OneDriveFolder folder = new Folder.OneDriveFolder(item);
                    foreach (var fi in items)
                    {
                        files = folder.AddFiles((Dictionary <string, object>)fi);
                        file.Add(files);
                    }
                }
                else if (item.token.Drive == "DropBox")
                {
                    var parameter = new HttpParameterCollection()
                    {
                        { "query", serchname },
                        { "access_token", item.token.access_token }
                    };
                    var result = OAuthUtility.Get("https://api.dropboxapi.com/1/search/auto//", parameter);
                    googlecloud1.Folder.DropBoxFolder folder = new Folder.DropBoxFolder(item);
                    foreach (RequestResult re in result)
                    {
                        files = folder.AddFiles(re.ToDictionary());
                        file.Add(files);
                    }
                }
            }
        }
Beispiel #6
0
 public static void DeleteAsync(string endpoint = null, HttpParameterCollection parameters                   = null,
                                HttpAuthorization authorization = null, NameValueCollection headers          = null, string contentType = null,
                                AccessToken accessToken         = null, ExecuteRequestAsyncCallback callback = null, bool allowWriteStreamBuffering = false,
                                bool allowSendChunked           = true, long contentLength = -1, HttpWriteRequestStream streamWriteCallback         = null,
                                int writeBufferSize             = 4096, int readBufferSize = 4096, bool donotEncodeKeys = false)
 {
     NewOAuthUtility.ExecuteRequestAsync("DELETE", endpoint, parameters, authorization, headers, contentType, accessToken,
                                         callback, allowWriteStreamBuffering, allowSendChunked, contentLength, streamWriteCallback, writeBufferSize,
                                         readBufferSize, donotEncodeKeys);
 }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (Session["AccessToken"] == null)
            {
                Response.Write("Error. Access token not found.<br /><a href=\"/\">Try again</a>.");
                pnlUpload.Visible = false;
                return;
            }

            // help: https://developers.google.com/drive/v2/reference/files/insert

            var token = Session["AccessToken"].ToString();

            try
            {
                /*
                 * // simple upload
                 * var result = OAuthUtility.Post
                 * (
                 * "https://www.googleapis.com/upload/drive/v2/files",
                 * new HttpParameterCollection
                 * {
                 *  { "uploadType", "media" },
                 *  { FileUpload1.PostedFile }
                 * },
                 * new HttpAuthorization(AuthorizationType.Bearer, token)
                 * );
                 */

                var parameters = new HttpParameterCollection();
                parameters.Add("uploadType", "multipart");
                parameters.AddContent("application/json", new { title = FileUpload1.FileName });
                parameters.AddContent(FileUpload1.PostedFile);

                var result = OAuthUtility.Post
                             (
                    "https://www.googleapis.com/upload/drive/v2/files",
                    parameters,
                    authorization: new HttpAuthorization(AuthorizationType.Bearer, token),
                    contentType: "multipart/related"
                             );

                // ok
                hlResult.NavigateUrl = result["webContentLink"].ToString();
                hlResult.Text        = hlResult.NavigateUrl;

                pnlSuccess.Visible = true;
                pnlUpload.Visible  = false;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
      if (Session["AccessToken"] == null)
      {
        Response.Write("Error. Access token not found.<br /><a href=\"/\">Try again</a>.");
        pnlUpload.Visible = false;
        return;
      }

      // help: https://developers.google.com/drive/v2/reference/files/insert

      var token = Session["AccessToken"].ToString();

      try
      {
        /*
        // simple upload
        var result = OAuthUtility.Post
        (
          "https://www.googleapis.com/upload/drive/v2/files",
          new HttpParameterCollection
          { 
            { "uploadType", "media" },
            { FileUpload1.PostedFile } 
          },
          new HttpAuthorization(AuthorizationType.Bearer, token)
        );
        */

        var parameters = new HttpParameterCollection();
        parameters.Add("uploadType", "multipart");
        parameters.AddContent("application/json", new { title = FileUpload1.FileName });
        parameters.AddContent(FileUpload1.PostedFile);

        var result = OAuthUtility.Post
        (
          "https://www.googleapis.com/upload/drive/v2/files",
          parameters, 
          authorization: new HttpAuthorization(AuthorizationType.Bearer, token),
          contentType: "multipart/related"
        );

        // ok
        hlResult.NavigateUrl = result["webContentLink"].ToString();
        hlResult.Text = hlResult.NavigateUrl;
          
        pnlSuccess.Visible = true;
        pnlUpload.Visible = false;
      }
      catch (Exception ex)
      {
        Response.Write(ex.Message);
      }
    }
        /// <summary>
        /// Uploads media (only photos).
        /// </summary>
        /// <remarks>
        /// <see href="https://dev.twitter.com/rest/reference/post/media/upload-init"/>
        /// </remarks>
        /// <param name="path">File path.</param>
        public static async Task <RequestResult> UploadMedia(string path, MeadiaUploadEventHandler uploadingCallback)
        {
            var file = new FileInfo(path);

            string media_type = "image/jpeg";

            switch (file.Extension.ToLower())
            {
            case ".png":
                media_type = "image/png";
                break;

            case ".gif":
                media_type = "image/gif";
                break;

            case ".bmp":
                media_type = "image/bmp";
                break;
            }

            var parameters = new HttpParameterCollection();

            parameters.AddFormParameter("command", "INIT");
            parameters.AddFormParameter("total_bytes", file.Length.ToString());
            parameters.AddFormParameter("media_type", media_type);
            parameters.AddFormParameter("media_category", "tweet_image");

            var t = Task.Run <RequestResult>(() =>
            {
                return(OAuthUtility.Post
                       (
                           "https://upload.twitter.com/1.1/media/upload.json",
                           parameters: parameters,
                           authorization: TwitterApi.GetAuth(),
                           contentType: "multipart/form-data"
                       ));
            });

            var result = await t;

            if (result.IsSuccessfully)
            {
                return(await TwitterApi.MeadiaUploadAppend(path, result["media_id"].ToString(), 0, uploadingCallback));
            }
            else
            {
                return(result);
            }
        }
Beispiel #10
0
 public async Task AddFiles(string id)
 {
     try
     {
         GoogleFiles.Clear();
         string query     = "'" + id + "' in parents";
         var    parameter = new HttpParameterCollection
         {
             { "q", query },
             { "access_token", driveinfo.token.access_token }
         };
         var result = OAuthUtility.Get("https://www.googleapis.com/drive/v2/files", parameter);
         var map    = new ApiDataMapping();
         map.Add("id", "FileID");
         map.Add("title", "FileName");
         map.Add("fileExtension", "Extention");
         map.Add("downloadUrl", "DownUrl");
         map.Add("thumbnailLink", "Thumnail");
         map.Add("fileSize", "FileSize", typeof(long));
         map.Add("description", "Description");
         map.Add("modifiedDate", "modifiedDate");
         foreach (var item in result.CollectionItems.Items["items"].CollectionItems.Items.Values)
         {
             FileInfo fi = new FileInfo(item, map);
             fi.driveinfo = driveinfo;
             if (id == "root")
             {
                 fi.Path = "Google/";
             }
             if (fi.Extention != null)
             {
                 fi.DownUrl = "https://www.googleapis.com/drive/v2/files/" + fi.FileID + "?alt=media";
                 fi.IsFile  = true;
             }
             else
             {
                 fi.Path  += fi.FileName + "/";
                 fi.IsFile = false;
             }
             GoogleFile itemss = new GoogleFile(fi);
             GoogleFiles.Add(itemss);
         }
     }
     catch (Exception e)
     {
         MessageBox.Show(string.Format("구글 파일 불러오기 오류 : {0}", e));
     }
 }
Beispiel #11
0
        private void button2_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;

            if (openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            object parents = null;

            if (!String.IsNullOrEmpty(this.CurrentFolderId))
            {
                parents = new object[] { new { id = this.CurrentFolderId } };
            }

            UniValue properties = UniValue.Create
                                  (
                new
            {
                title   = Path.GetFileName(openFileDialog1.FileName),
                parents = parents
            }
                                  );

            var file = openFileDialog1.OpenFile();

            this.CurrentFileLength = file.Length;

            var parameters = new HttpParameterCollection();

            parameters.Add("uploadType", "multipart");
            parameters.AddContent("application/json", properties.ToString());
            parameters.AddContent("application/octet-stream", file);

            OAuthUtility.PostAsync
            (
                "https://www.googleapis.com/upload/drive/v2/files",
                parameters,
                authorization: new HttpAuthorization(AuthorizationType.Bearer, Properties.Settings.Default.AccessToken),
                contentType: "multipart/related",
                // handler of result
                callback: Upload_Result,
                // handler of uploading
                streamWriteCallback: Upload_Processing
            );
        }
Beispiel #12
0
 public async Task AddFiles(string id)
 {
     try
     {
         var parameter = new HttpParameterCollection()
         {
             { "access_token", driveinfo.token.access_token }
         };
         var result = OAuthUtility.Get(string.Format("https://api.onedrive.com/v1.0/drive/items/{0}/children", id), parameter);
         var map    = new ApiDataMapping();
         map.Add("id", "FileID");
         map.Add("name", "FileName");
         map.Add("@content.downloadUrl", "DownUrl");
         map.Add("size", "FileSize", typeof(long));
         map.Add("lastModifiedDateTime", "modifiedDate");
         foreach (var item in result.Result.CollectionItems.Items["value"].CollectionItems.Items.Values)
         {
             FileInfo fi = new FileInfo(item, map);
             fi.driveinfo = driveinfo;
             if (id == "root")
             {
                 fi.Path = "OneDrive/";
             }
             if (fi.Items.CollectionItems.ContainsKey("folder"))
             {
                 fi.IsFile = false;
                 fi.Path  += fi.FileName + "/";
             }
             else
             {
                 fi.IsFile    = true;
                 fi.Extention = GetExtension(fi.FileName);
                 fi.DownUrl   = item.CollectionItems["@content.downloadUrl"].ToString();
             }
             fi.Thumnail = GetTumbNail(fi.FileID);
             OneDriveFile items = new OneDriveFile(fi);
             files.Add(items);
         }
     }
     catch (Exception e)
     {
         MessageBox.Show(string.Format("원드라이브 파일 불러오기 오류 : {0}", e));
     }
 }
Beispiel #13
0
        private void GetTweets()
        {
            this.Cursor = Cursors.WaitCursor;

            var parameters = new HttpParameterCollection();

            if (!String.IsNullOrEmpty(this.LastTweetId))
            {
                parameters.AddUrlParameter("max_id", this.LastTweetId);
            }

            OAuthUtility.GetAsync
            (
                "https://api.twitter.com/1.1/statuses/user_timeline.json",
                parameters: parameters,
                authorization: this.GetAuth(),
                callback: GetTweets_Result
            );
        }
        /// <summary>
        /// https://dev.twitter.com/rest/reference/post/media/upload-finalize
        /// </summary>
        /// <param name="media_id">Media id.</param>
        private static async Task <RequestResult> MeadiaUploadFinalize(string path, string media_id)
        {
            var parameters = new HttpParameterCollection();

            parameters.AddFormParameter("command", "FINALIZE");
            parameters.AddFormParameter("media_id", media_id);

            var t = Task.Run <RequestResult>(() =>
            {
                return(OAuthUtility.Post
                       (
                           "https://upload.twitter.com/1.1/media/upload.json",
                           parameters: parameters,
                           authorization: TwitterApi.GetAuth(),
                           contentType: "multipart/form-data"
                       ));
            });

            return(await t);
        }
Beispiel #15
0
        /// <summary>
        /// Sends a request to refresh the access token.
        /// </summary>
        /// <param name="accessToken">May contain an access token, which should be refreshed.</param>
        /// <param name="returnUrl">Callback address that was used in obtaining the access token.</param>
        /// <remarks>
        /// <para>If <paramref name="accessToken"/> parameter is not specified, it will use the current access token from the same property of the current class instance.</para>
        /// <para>Token must contain the <b>refresh_token</b>, which was received together with the access token.</para>
        /// <list type="table">
        /// <item>
        /// <term><img src="../img/warning.png" alt="(!)" title="" /></term>
        /// <term><b>To update the access token, you must specify the return address that was used in obtaining the access token.</b></term>
        /// </item>
        /// </list>
        /// </remarks>
        /// <exception cref="NotSupportedException">
        /// <para>Provider does not support refreshing the access token, or the method is not implemented.</para>
        /// <para>Use the property <see cref="OAuthBase.SupportRefreshToken"/>, to check the possibility of calling this method.</para>
        /// </exception>
        /// <exception cref="AccessTokenException">
        /// <para>Access token is not found or is not specified.</para>
        /// <para>-or-</para>
        /// <para><b>refresh_token</b> value is empty.</para>
        /// </exception>
        /// <exception cref="RequestException">Error during execution of a web request.</exception>
        /// <exception cref="ArgumentNullException">An exception occurs if there is no authorization code.</exception>
        public AccessToken RefreshToken(AccessToken accessToken, string returnUrl)
        {
            var token = (OAuth2AccessToken)base.GetSpecifiedTokenOrCurrent(accessToken, refreshTokenRequired: true);

            var parameters = new HttpParameterCollection();

            parameters.AddFormParameter("client_id", this.ApplicationId);
            parameters.AddFormParameter("client_secret", this.ApplicationSecret);
            parameters.AddFormParameter("redirect_uri", returnUrl);
            parameters.AddFormParameter("grant_type", GrantType.RefreshToken);
            parameters.AddFormParameter("refresh_token", token.RefreshToken);

            var result = OAuthUtility.Post
                         (
                this.AccessTokenUrl,
                parameters: parameters,
                authorization: new HttpAuthorization(AuthorizationType.Basic, OAuthUtility.ToBase64String("{0}:{1}", this.ApplicationId, this.ApplicationSecret))
                         );

            return(new OAuth2AccessToken(result));
        }
        private static readonly int MediaUploadChunkSize = (3 * 1024 * 1024); // 3 MB

        /// <summary>
        /// Checks access token.
        /// </summary>
        public static async Task <bool> CheckAccessToken()
        {
            var parameters = new HttpParameterCollection
            {
                new HttpUrlParameter("user_id", Properties.Settings.Default.UserId),
                new HttpUrlParameter("include_entities", "false")
            };

            var t = Task.Run <bool>(() =>
            {
                var result = OAuthUtility.Get
                             (
                    "https://api.twitter.com/1.1/users/show.json",
                    parameters: parameters,
                    authorization: TwitterApi.GetAuth()
                             );

                return(result.IsSuccessfully);
            });

            return(await t);
        }
        /// <summary>
        /// Gets tweets.
        /// </summary>
        /// <param name="lastTweetId"></param>
        /// <remarks>
        /// <see href="https://dev.twitter.com/rest/reference/get/statuses/user_timeline"/>
        /// </remarks>
        public static async Task <RequestResult> GetTweets(string lastTweetId)
        {
            HttpParameterCollection parameters = null;

            if (!String.IsNullOrEmpty(lastTweetId))
            {
                parameters = new HttpParameterCollection {
                    new HttpUrlParameter("max_id", lastTweetId)
                };
            }

            var t = Task.Run <RequestResult>(() =>
            {
                return(OAuthUtility.Get
                       (
                           "https://api.twitter.com/1.1/statuses/user_timeline.json",
                           parameters: parameters,
                           authorization: TwitterApi.GetAuth()
                       ));
            });

            return(await t);
        }
        /// <summary>
        /// Gets the user details.
        /// </summary>
        /// <param name="accessToken">May contain an access token, which will have to be used in obtaining information about the user.</param>
        public override UserInfo GetUserInfo(AccessToken accessToken = null)
        {
            // help: https://dev.twitter.com/rest/reference/get/users/show

            accessToken = base.GetSpecifiedTokenOrCurrent(accessToken);

            string url = "https://api.twitter.com/1.1/users/show.json";

            // query parameters
            var parameters = new HttpParameterCollection();

            parameters.AddUrlParameter("user_id", accessToken["user_id"].ToString());
            parameters.AddUrlParameter("screen_name", accessToken["screen_name"].ToString());
            parameters.AddUrlParameter("include_entities", "false");

            this.Authorization["oauth_token"] = accessToken["oauth_token"].ToString();
            this.Authorization.TokenSecret    = accessToken["oauth_token_secret"].ToString();

            // execute the request
            var result = OAuthUtility.Get(url, parameters, this.Authorization);

            // field mapping
            var map = new ApiDataMapping();

            map.Add("id_str", "UserId", typeof(string));
            map.Add("name", "DisplayName");
            map.Add("screen_name", "UserName");
            map.Add("profile_image_url", "Userpic");
            map.Add("url", "Url");
            map.Add("birthday", "Birthday", typeof(DateTime), @"dd\.MM\.yyyy");
            map.Add("lang", "Language");
            //map.Add("verified", "Url");
            //map.Add("location", "Url");

            // parse the server response and returns the UserInfo instance
            return(new UserInfo(result, map));
        }
Beispiel #19
0
        private void button1_Click(object sender, EventArgs e)
        {
            // help: https://developers.google.com/drive/v2/reference/files/insert

            object parents = null;

            if (!String.IsNullOrEmpty(this.CurrentFolderId))
            {
                parents = new object[] { new { id = this.CurrentFolderId } };
            }

            UniValue content = UniValue.Create
                               (
                new
            {
                mimeType = "application/vnd.google-apps.folder",
                title    = textBox1.Text,
                parents  = parents
            }
                               );

            var parameters = new HttpParameterCollection();

            parameters.Encoding = Encoding.UTF8;
            parameters.Add("uploadType", "multipart");
            parameters.AddContent("application/json", content.ToString());

            OAuthUtility.PostAsync
            (
                "https://www.googleapis.com/upload/drive/v2/files",
                parameters: parameters,
                authorization: new HttpAuthorization(AuthorizationType.Bearer, Properties.Settings.Default.AccessToken),
                contentType: "multipart/related",
                callback: CreateFolder_Result
            );
        }
 /// <summary>
 /// Performs an async request.
 /// </summary>
 /// <param name="method">HTTP Method: <b>POST</b> (default), <b>PUT</b>, <b>GET</b> or <b>DELETE</b>.</param>
 /// <param name="endpoint">URL to which will be sent to request.</param>
 /// <param name="parameters">Parameters to be passed to request.</param>
 /// <param name="authorization">Authorization header value.</param>
 /// <param name="headers">HTTP headers for request.</param>
 /// <param name="contentType">The value of the <b>Content-Type</b> HTTP header.</param>
 /// <param name="callback">A delegate that, if provided, is called when an async web request is completed.</param>
 /// <param name="accessToken">Access token to be used in the request.</param>
 /// <remarks>
 /// <para>Can not be used simultaneously <paramref name="accessToken"/> and <paramref name="authorization"/>. Use only one of these parameters.</para>
 /// </remarks>
 /// <returns>Returns an instance of the <see cref="RequestResult"/> class, which contains the result of the request.</returns>
 /// <exception cref="System.ArgumentNullException"></exception>
 /// <exception cref="RequestException"></exception>
 /// <exception cref="ArgumentException">
 /// <para>The exception occurs when the query parameters are specified at the same time <paramref name="authorization"/> and <paramref name="accessToken"/>.</para>
 /// </exception>
 public static void ExecuteRequestAsync(string method = "POST", string endpoint = null, HttpParameterCollection parameters = null, HttpAuthorization authorization = null, NameValueCollection headers = null, string contentType = null, AccessToken accessToken = null, ExecuteRequestAsyncCallback callback = null)
 {
   var t = new Thread
   (() =>
   {
     RequestResult result = null;
     try
     {
       result = OAuthUtility.ExecuteRequest(method, endpoint, parameters, authorization, headers, contentType, accessToken);
     }
     catch (RequestException ex)
     {
       result = ex.RequestResult;
     }
     if (callback != null)
     {
       callback(result);
     }
   }
   );
   t.IsBackground = true;
   t.Start();
 }
        /// <summary>
        /// https://dev.twitter.com/rest/reference/post/media/upload-append
        /// </summary>
        /// <param name="path">File path.</param>
        /// <param name="media_id">Media id.</param>
        /// <param name="chunk">Chunk. Default: 0.</param>
        /// <param name="uploadingCallback">Uploading callback.</param>
        private static async Task <RequestResult> MeadiaUploadAppend(string path, string media_id, int chunk, MeadiaUploadEventHandler uploadingCallback)
        {
            var  file      = new FileInfo(path);
            bool isUploded = false;

            byte[] media = null;

            if (chunk > 0)
            {
                // multiple chunks
                using (var stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.Inheritable))
                    using (var reader = new BinaryReader(stream))
                    {
                        stream.Position = (TwitterApi.MediaUploadChunkSize * chunk);
                        media           = reader.ReadBytes(TwitterApi.MediaUploadChunkSize);
                        isUploded       = (stream.Position == stream.Length);
                    }
            }
            else
            {
                if (file.Length <= TwitterApi.MediaUploadChunkSize)
                {
                    // one chunk
                    using (var reader = new BinaryReader(file.Open(FileMode.Open, FileAccess.Read, FileShare.Inheritable)))
                    {
                        media     = reader.ReadBytes(Convert.ToInt32(file.Length));
                        isUploded = true;
                    }
                }
                else
                {
                    // multiple chunks
                    using (var stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.Inheritable))
                        using (var reader = new BinaryReader(stream))
                        {
                            media     = reader.ReadBytes(TwitterApi.MediaUploadChunkSize);
                            isUploded = (stream.Position == stream.Length);
                        }
                }
            }

            var parameters = new HttpParameterCollection();

            parameters.AddFormParameter("command", "APPEND");
            parameters.AddFormParameter("media_id", media_id);
            parameters.AddFormParameter("segment_index", chunk.ToString());
            parameters.Add("media", Path.GetFileName(path), media);

            var t = Task.Run <RequestResult>(() =>
            {
                return(OAuthUtility.Post
                       (
                           "https://upload.twitter.com/1.1/media/upload.json",
                           parameters: parameters,
                           authorization: TwitterApi.GetAuth(),
                           contentType: "multipart/form-data",
                           streamWriteCallback: (object s, StreamWriteEventArgs e) =>
                {
                    int progressPercentage = 0;
                    long totalUploaded = 0;

                    if (chunk > 0)
                    {
                        totalUploaded = TwitterApi.MediaUploadChunkSize * chunk;
                    }

                    totalUploaded += e.TotalBytesWritten;

                    progressPercentage = Convert.ToInt32((totalUploaded * 100) / file.Length);

                    uploadingCallback?.Invoke(s, new ProgressChangedEventArgs(progressPercentage, null));
                }
                       ));
            });

            var result = await t;

            if (!result.IsSuccessfully)
            {
                // error
                return(result);
            }

            if (file.Length > TwitterApi.MediaUploadChunkSize && !isUploded)
            {
                // next chunk
                return(await TwitterApi.MeadiaUploadAppend(path, media_id, chunk + 1, uploadingCallback));
            }
            else
            {
                // finalize
                return(await TwitterApi.MeadiaUploadFinalize(path, media_id));
            }
        }
Beispiel #22
0
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (Session["AccessToken"] == null)
            {
                Response.Write("Error. Access token not found.<br /><a href=\"/\">Try again</a>.");
                pnlUpload.Visible = false;
                return;
            }

            // help: https://developers.google.com/drive/v2/reference/files/insert

            var token    = Session["AccessToken"].ToString();
            var filePath = Server.MapPath(String.Format("~/Temp/{0}.tmp", Guid.NewGuid()));

            try
            {
                /*
                 * // simple upload
                 * var result = OAuthUtility.Post
                 * (
                 * "https://www.googleapis.com/upload/drive/v2/files",
                 * new HttpParameterCollection
                 * {
                 *  { "uploadType", "media" },
                 *  { FileUpload1.PostedFile }
                 * },
                 * new HttpAuthorization(AuthorizationType.Bearer, token)
                 * );
                 */

                // save file
                if (!Directory.Exists(Server.MapPath("~/Temp")))
                {
                    Directory.CreateDirectory(Server.MapPath("~/Temp"));
                }

                using (var file = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.Inheritable))
                    using (var writer = new BinaryWriter(file))
                    {
                        using (var reader = new BinaryReader(FileUpload1.PostedFile.InputStream))
                        {
                            byte[] buffer    = new byte[4096];
                            int    readBytes = 0;

                            while ((readBytes = reader.Read(buffer, 0, buffer.Length)) != 0)
                            {
                                writer.Write(buffer, 0, readBytes);
                            }
                        }
                    }

                var fileToSend = File.OpenRead(filePath);

                // send saved file
                var parameters = new HttpParameterCollection();
                parameters.Add("uploadType", "multipart");
                parameters.AddContent("application/json", new { title = FileUpload1.FileName });
                parameters.AddContent
                (
                    FileUpload1.PostedFile.ContentType ?? "application/octet-stream",
                    fileToSend
                );

                var result = OAuthUtility.Post
                             (
                    "https://www.googleapis.com/upload/drive/v2/files",
                    parameters,
                    authorization: new HttpAuthorization(AuthorizationType.Bearer, token),
                    contentType: "multipart/related"
                             );

                fileToSend.Close();

                // ok
                hlResult.NavigateUrl = result["webContentLink"].ToString();
                hlResult.Text        = hlResult.NavigateUrl;

                pnlSuccess.Visible = true;
                pnlUpload.Visible  = false;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                // remove temp file
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
        }
    /// <summary>
    /// Sends a request to refresh the access token.
    /// </summary>
    /// <param name="accessToken">May contain an access token, which should be refreshed.</param>
    /// <param name="returnUrl">Callback address that was used in obtaining the access token.</param>
    /// <remarks>
    /// <para>If <paramref name="accessToken"/> parameter is not specified, it will use the current access token from the same property of the current class instance.</para>
    /// <para>Token must contain the <b>refresh_token</b>, which was received together with the access token.</para>
    /// <list type="table">
    /// <item>
    /// <term><img src="../img/warning.png" alt="(!)" title="" /></term>
    /// <term><b>To update the access token, you must specify the return address that was used in obtaining the access token.</b></term>
    /// </item>
    /// </list>
    /// </remarks>
    /// <exception cref="NotSupportedException">
    /// <para>Provider does not support refreshing the access token, or the method is not implemented.</para>
    /// <para>Use the property <see cref="OAuthBase.SupportRefreshToken"/>, to check the possibility of calling this method.</para>
    /// </exception>
    /// <exception cref="AccessTokenException">
    /// <para>Access token is not found or is not specified.</para>
    /// <para>-or-</para>
    /// <para><b>refresh_token</b> value is empty.</para>
    /// </exception>
    /// <exception cref="RequestException">Error during execution of a web request.</exception>
    /// <exception cref="ArgumentNullException">An exception occurs if there is no authorization code.</exception>
    public AccessToken RefreshToken(AccessToken accessToken, string returnUrl)
    {
      var token = (OAuth2AccessToken)base.GetSpecifiedTokenOrCurrent(accessToken, refreshTokenRequired: true);

      var parameters = new HttpParameterCollection();
      parameters.AddFormParameter("client_id", this.ApplicationId);
      parameters.AddFormParameter("client_secret", this.ApplicationSecret);
      parameters.AddFormParameter("redirect_uri", returnUrl);
      parameters.AddFormParameter("grant_type", GrantType.RefreshToken);
      parameters.AddFormParameter("refresh_token", token.RefreshToken);

      var result = OAuthUtility.Post
      (
        this.AccessTokenUrl,
        parameters: parameters,
        authorization: new HttpAuthorization(AuthorizationType.Basic, OAuthUtility.ToBase64String("{0}:{1}", this.ApplicationId, this.ApplicationSecret))
      );

      return new OAuth2AccessToken(result);
    }
    /// <summary>
    /// Gets the user details.
    /// </summary>
    /// <param name="accessToken">May contain an access token, which will have to be used in obtaining information about the user.</param>
    public override UserInfo GetUserInfo(AccessToken accessToken = null)
    {
      // help: https://dev.twitter.com/docs/api/1/get/users/show

      accessToken = base.GetSpecifiedTokenOrCurrent(accessToken);

      string url = "https://api.twitter.com/1.1/users/show.json";

      // query parameters
      var parameters = new HttpParameterCollection();
      parameters.AddUrlParameter("user_id", accessToken["user_id"].ToString());
      parameters.AddUrlParameter("screen_name", accessToken["screen_name"].ToString());
      parameters.AddUrlParameter("include_entities", "false");

      this.Authorization["oauth_token"] = accessToken["oauth_token"].ToString();
      this.Authorization.TokenSecret = accessToken["oauth_token_secret"].ToString();

      // execute the request
      var result = OAuthUtility.Get(url, parameters, this.Authorization);

      // field mapping
      var map = new ApiDataMapping();
      map.Add("id_str", "UserId", typeof(string));
      map.Add("name", "DisplayName");
      map.Add("screen_name", "UserName");
      map.Add("profile_image_url", "Userpic");
      map.Add("url", "Url");
      map.Add("birthday", "Birthday", typeof(DateTime), @"dd\.MM\.yyyy");
      //map.Add("verified", "Url");
      //map.Add("location", "Url");

      // parse the server response and returns the UserInfo instance
      return new UserInfo(result, map);
    }
 /// <summary>
 /// Performs a request using a <b>PUT</b> method.
 /// </summary>
 /// <param name="endpoint">URL to which will be sent to the request.</param>
 /// <param name="parameters">Parameters to be passed to the request.</param>
 /// <param name="authorization">Authorization header value.</param>
 /// <param name="headers">HTTP headers for the request.</param>
 /// <param name="contentType">The value of the <b>Content-Type</b> HTTP header.</param>
 /// <param name="accessToken">Access token to be used in the request.</param>
 /// <remarks>
 /// <para>Can not be used simultaneously <paramref name="accessToken"/> and <paramref name="authorization"/>. Use only one of these parameters.</para>
 /// </remarks>
 /// <returns>Returns an instance of the <see cref="RequestResult"/> class, which contains the result of the request.</returns>
 /// <exception cref="System.ArgumentNullException"></exception>
 /// <exception cref="RequestException"></exception>
 /// <exception cref="ArgumentException">
 /// <para>The exception occurs when the query parameters are specified at the same time <paramref name="authorization"/> and <paramref name="accessToken"/>.</para>
 /// </exception>
 public static RequestResult Put(string endpoint = null, HttpParameterCollection parameters = null, HttpAuthorization authorization = null, NameValueCollection headers = null, string contentType = null, AccessToken accessToken = null)
 {
   return OAuthUtility.ExecuteRequest("PUT", endpoint, parameters, authorization, headers, contentType, accessToken);
 }
Beispiel #26
0
        public void HttpParametersWriteTest()
        {
            Console.WriteLine("Test 1");
            object obj        = null;
            string str        = null;
            int?   @int       = null;
            var    parameters = new HttpParameterCollection
            {
                { "test", obj },
                { "test2", str },
                { "test3", @int }
            };

            foreach (var p in parameters)
            {
                Assert.AreEqual(HttpParameterType.Unformed, p.ParameterType);
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }

            var req = (HttpWebRequest)WebRequest.Create("http://api.foxtools.ru/v2/hash");

            req.Method = "POST";

            parameters.WriteToRequestStream(req);

            string r = parameters.ToStringParameters();

            Assert.AreEqual("test=&test2=&test3=", r);

            Console.WriteLine(r);

            Console.WriteLine("-------------------------------------");
            Console.WriteLine("Test 2");

            parameters = new HttpParameterCollection
            {
                new HttpUrlParameter("test", "123"),
                new HttpFormParameter("test2", null),
                new HttpFormParameter("text", "test")
            };


            for (int i = 0; i < parameters.Count; i++)
            {
                var p = parameters[i];

                if (i == 0)
                {
                    Assert.AreEqual(HttpParameterType.Url, p.ParameterType);
                }
                else
                {
                    Assert.AreEqual(HttpParameterType.Form, p.ParameterType);
                }

                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }

            req        = (HttpWebRequest)WebRequest.Create("http://api.foxtools.ru/v2/hash");
            req.Method = "POST";

            parameters.WriteToRequestStream(req);

            r = parameters.ToStringParameters(HttpParameterType.Url);

            Assert.AreEqual("test=123", r);

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 3");

            parameters = new HttpParameterCollection
            {
                new { a = 1, b = 2, c = "abc" }
            };


            for (int i = 0; i < parameters.Count; i++)
            {
                var p = parameters[i];

                Assert.AreEqual(HttpParameterType.RequestBody, p.ParameterType);

                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }

            req             = (HttpWebRequest)WebRequest.Create("http://api.foxtools.ru/v2/hash");
            req.Method      = "POST";
            req.ContentType = "application/json";

            parameters.WriteToRequestStream(req);

            req.GetResponse();

            req        = (HttpWebRequest)WebRequest.Create("http://api.foxtools.ru/v2/hash");
            req.Method = "POST";

            parameters.WriteToRequestStream(req);

            req.GetResponse();

            Console.WriteLine("-------------------------------------");
        }
 /// <summary>
 /// Performs an async request using a <b>PUT</b> method.
 /// </summary>
 /// <param name="endpoint">URL to which will be sent to request.</param>
 /// <param name="parameters">Parameters to be passed to request.</param>
 /// <param name="authorization">Authorization header value.</param>
 /// <param name="headers">HTTP headers for request.</param>
 /// <param name="contentType">The value of the <b>Content-Type</b> HTTP header.</param>
 /// <param name="callback">A delegate that, if provided, is called when an async request is completed.</param>
 /// <param name="accessToken">Access token to be used in the request.</param>
 /// <remarks>
 /// <para>Can not be used simultaneously <paramref name="accessToken"/> and <paramref name="authorization"/>. Use only one of these parameters.</para>
 /// </remarks>
 /// <returns>Returns an instance of the <see cref="RequestResult"/> class, which contains the result of the request.</returns>
 /// <exception cref="System.ArgumentNullException"></exception>
 /// <exception cref="RequestException"></exception>
 /// <exception cref="ArgumentException">
 /// <para>The exception occurs when the query parameters are specified at the same time <paramref name="authorization"/> and <paramref name="accessToken"/>.</para>
 /// </exception>
 public static void PutAsync(string endpoint = null, HttpParameterCollection parameters = null, HttpAuthorization authorization = null, NameValueCollection headers = null, string contentType = null, AccessToken accessToken = null, ExecuteRequestAsyncCallback callback = null)
 {
   OAuthUtility.ExecuteRequestAsync("PUT", endpoint, parameters, authorization, headers, contentType, accessToken, callback);
 }
 public void SetSignature(string httpMethod, string url, string applicationSecret, string tokenSecret, HttpParameterCollection parameters)
 {
   this.SetSignature(httpMethod, url, applicationSecret, tokenSecret, parameters);
 }
 /// <summary>
 /// Invoked before sending a web request.
 /// </summary>
 /// <param name="httpMethod">HTTP Method of the request: <b>POST</b>, <b>PUT</b>, <b>GET</b> or <b>DELETE</b>.</param>
 /// <param name="url">URL of the web request.</param>
 /// <param name="contentType">The value of the <b>Content-Type</b> HTTP header.</param>
 /// <param name="parameters">>Parameters of the web request.</param>
 internal virtual void Build(string httpMethod, string url, string contentType, HttpParameterCollection parameters) { }
    /// <summary>
    /// Sends a request to refresh the access token.
    /// </summary>
    /// <param name="accessToken">May contain an access token, which should be refreshed.</param>
    /// <remarks>
    /// <para>If <paramref name="accessToken"/> parameter is not specified, it will use the current access token from the same property of the current class instance.</para>
    /// <para>Token must contain the <b>refresh_token</b>, which was received together with the access token.</para>
    /// </remarks>
    /// <exception cref="NotSupportedException">
    /// <para>Provider does not support refreshing the access token, or the method is not implemented.</para>
    /// <para>Use the property <see cref="OAuthBase.SupportRefreshToken"/>, to check the possibility of calling this method.</para>
    /// </exception>
    /// <exception cref="AccessTokenException">
    /// <para>Access token is not found or is not specified.</para>
    /// <para>-or-</para>
    /// <para><b>refresh_token</b> value is empty.</para>
    /// </exception>
    /// <exception cref="RequestException">Error during execution of a web request.</exception>
    public override AccessToken RefreshToken(AccessToken accessToken = null)
    {
      if (!this.SupportRefreshToken)
      {
        throw new NotSupportedException();
      }

      var token = (OAuth2AccessToken)base.GetSpecifiedTokenOrCurrent(accessToken, refreshTokenRequired: true);

      var parameters = new HttpParameterCollection();
      parameters.AddFormParameter("access_token", token.Value);
      parameters.AddFormParameter("client_id", this.ApplicationId);
      parameters.AddFormParameter("client_secret", this.ApplicationSecret);
      parameters.AddFormParameter("grant_type", GrantType.RefreshToken);
      parameters.AddFormParameter("refresh_token",  token.RefreshToken);

      var result = OAuthUtility.Post
      (
        this.AccessTokenUrl,
        parameters: parameters
      );

      return new OAuth2AccessToken(result);
    }
Beispiel #31
0
        public void HttpParametersTest()
        {
            Console.WriteLine("Test 1");
            object obj        = null;
            string str        = null;
            int?   @int       = null;
            var    parameters = new HttpParameterCollection
            {
                { "test", obj },
                { "test2", str },
                { "test3", @int }
            };

            foreach (var p in parameters)
            {
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }
            Console.WriteLine(parameters.ToStringParameters());

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 2");
            parameters = new HttpParameterCollection
            {
                { "n", 1 },
                { "n", 2 },
                { "n", 3 }
            };

            foreach (var p in parameters)
            {
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }
            Console.WriteLine(parameters.ToStringParameters());

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 3");
            parameters = new HttpParameterCollection
            {
                { "n[]", 1 },
                { "n[]", 2 },
                { "n[]", 3 }
            };

            foreach (var p in parameters)
            {
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }
            Console.WriteLine(parameters.ToStringParameters());

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 4");

            var constructorInfo = typeof(System.Web.HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First();
            var f = (System.Web.HttpPostedFile)constructorInfo.Invoke(new object[] { "oauth.png", "image/png", null });

            parameters = new HttpParameterCollection
            {
                { "file", "test.dat", "text/plain", new byte[] { 1, 2, 3, 4, 5 } },
                { "file2", "test2.dat", new byte[] { 1, 2, 3, 4, 5 } },
                { "file3", "test3.dat", new MemoryStream(Properties.Resources.oauth) },
                { "file4", f },
                { "x", 123 },
                { "y", 456 }
            };

            foreach (var p in parameters)
            {
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }
            Console.WriteLine(parameters.ToStringParameters());

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 5");
            parameters = new HttpParameterCollection
            {
                { new byte[] { 1, 2, 3, 4, 5 } },
                { "x", 123 },
                { "y", 456 }
            };

            foreach (var p in parameters)
            {
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }
            Console.WriteLine(parameters.ToStringParameters());

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 6");
            parameters = new HttpParameterCollection
            {
                { f },
                { "x", 123 },
                { "y", 456 }
            };

            foreach (var p in parameters)
            {
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }
            Console.WriteLine(parameters.ToStringParameters());

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 7");
            parameters = new HttpParameterCollection
            {
                { Properties.Resources.oauth },
                { "x", 123 },
                { "x", 789 },
                { "y", 456 }
            };

            foreach (var p in parameters)
            {
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }
            Console.WriteLine(parameters.ToStringParameters());

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 8");
            object obj2 = Properties.Resources.oauth;

            parameters = new HttpParameterCollection
            {
                { obj2 }
            };

            foreach (var p in parameters)
            {
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }
            Console.WriteLine(parameters.ToStringParameters());

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 9");
            var parameters2 = new NameValueCollection
            {
                { "a", "123" },
                { "a", "245" },
                { "b", "abc" }
            };

            foreach (var p in (HttpParameterCollection)parameters2)
            {
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }
            Console.WriteLine(((HttpParameterCollection)parameters2).ToStringParameters());
        }
Beispiel #32
0
    public void HttpParametersTest()
    {
      Console.WriteLine("Test 1");
      object obj = null;
      string str = null;
      int? @int = null;
      var parameters = new HttpParameterCollection 
      {
        { "test", obj },
        { "test2", str },
        { "test3", @int }
      };

      foreach (var p in parameters)
      {
        Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
      }
      Console.WriteLine(parameters.ToStringParameters());

      Console.WriteLine("-------------------------------------");

      Console.WriteLine("Test 2");
      parameters = new HttpParameterCollection
      {
        { "n", 1 },
        { "n", 2 },
        { "n", 3 }
      };

      foreach (var p in parameters)
      {
        Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
      }
      Console.WriteLine(parameters.ToStringParameters());

      Console.WriteLine("-------------------------------------");

      Console.WriteLine("Test 3");
      parameters = new HttpParameterCollection
      {
        { "n[]", 1 },
        { "n[]", 2 },
        { "n[]", 3 }
      };

      foreach (var p in parameters)
      {
        Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
      }
      Console.WriteLine(parameters.ToStringParameters());

      Console.WriteLine("-------------------------------------");

      Console.WriteLine("Test 4");

      var constructorInfo = typeof(System.Web.HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First();
      var f = (System.Web.HttpPostedFile)constructorInfo.Invoke(new object[] { "oauth.png", "image/png", null });
      
      parameters = new HttpParameterCollection
      {
        { "file", "test.dat", "text/plain", new byte[] {1, 2, 3, 4, 5} },
        { "file2", "test2.dat", new byte[] {1, 2, 3, 4, 5} },
        { "file3", "test3.dat",  new MemoryStream(Properties.Resources.oauth) },
        { "file4", f },
        { "x", 123 },
        { "y", 456 }
      };

      foreach (var p in parameters)
      {
        Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
      }
      Console.WriteLine(parameters.ToStringParameters());

      Console.WriteLine("-------------------------------------");

      Console.WriteLine("Test 5");
      parameters = new HttpParameterCollection
      {
        { new byte[] {1, 2, 3, 4, 5} },
        { "x", 123 },
        { "y", 456 }
      };

      foreach (var p in parameters)
      {
        Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
      }
      Console.WriteLine(parameters.ToStringParameters());

      Console.WriteLine("-------------------------------------");

      Console.WriteLine("Test 6");
      parameters = new HttpParameterCollection
      {
        { f },
        { "x", 123 },
        { "y", 456 }
      };

      foreach (var p in parameters)
      {
        Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
      }
      Console.WriteLine(parameters.ToStringParameters());

      Console.WriteLine("-------------------------------------");

      Console.WriteLine("Test 7");
      parameters = new HttpParameterCollection
      {
        { Properties.Resources.oauth },
        { "x", 123 },
        { "x", 789 },
        { "y", 456 }
      };

      foreach (var p in parameters)
      {
        Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
      }
      Console.WriteLine(parameters.ToStringParameters());

      Console.WriteLine("-------------------------------------");

      Console.WriteLine("Test 8");
      object obj2 = Properties.Resources.oauth;
      parameters = new HttpParameterCollection
      {
        { obj2 }
      };

      foreach (var p in parameters)
      {
        Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
      }
      Console.WriteLine(parameters.ToStringParameters());

      Console.WriteLine("-------------------------------------");

      Console.WriteLine("Test 9");
      var parameters2 = new NameValueCollection
      {
        { "a", "123" },
        { "a", "245" },
        { "b", "abc" }
      };

      foreach (var p in (HttpParameterCollection)parameters2)
      {
        Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
      }
      Console.WriteLine(((HttpParameterCollection)parameters2).ToStringParameters());
    }
Beispiel #33
0
    public void ExecuteRequestTest()
    {
      Console.WriteLine("Test GET");
      var parameters = new HttpParameterCollection 
      {
        { "content", "hello world!" },
        { "contentType", "text/plain" }
      };
      var result = OAuthUtility.Get("http://api.foxtools.ru/v2/Http", parameters);

      if (result.ToString().IndexOf("hello world!") != -1)
      {
        Console.WriteLine("OK");
      }
      else
      {
        Console.WriteLine(result);
      }

      Console.WriteLine("-------------------------------------");

      Console.WriteLine("Test POST RequestBody");
      parameters = new HttpParameterCollection 
      {
        { Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAFUAAABVCAYAAAA49ahaAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAAR/SURBVHhe7ZDRbiMxDAPv/3+6px62gDomzGVsb+4hA0wNiJSS5k/x9aSO3X3mp70+U4endOzuMz/t9Zk6PKVjd5/5aa/P1OEpHbv7zE97feY43Im7n+ZO4vJV1P16x+FO3P00dxKXr6Lu1zsOd+Lup7mTuHwVdb/ecbgTdz/NncTlq6j79Y7DDnMnSXNKVKdL0txJVF7vOOwwd5I0p0R1uiTNnUTl9Y7DDnMnSXNKVKdL0txJVF7vOOwwd5I0p0R1uiTNnUTl9Y7DDnMnSfNUh+szdxKV1zsOO8ydJM1THa7P3ElUXu847DB3kjRPdbg+cydReb3jsMPcSdI81eH6zJ1E5fWOww5zJ3E5cX3mlKS5k6i83nHYYe4kLieuz5ySNHcSldc7DjvMncTlxPWZU5LmTqLyesdhh7mTuJy4PnNK0txJVF7vONzJu++7f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GtGAk5wAAAABJRU5ErkJggg==") },
        { "test", 123 },
        { new HttpFormParameter("x", "y") },
        { new HttpFormParameter("a", "bc") },
        { "contentType", "text/plain" }
      };
      result = OAuthUtility.Post("http://api.foxtools.ru/v2/Http", parameters);

      Assert.IsTrue(result.ToString().IndexOf("PNG") != -1);

      Console.WriteLine("-------------------------------------");

      Console.WriteLine("Test POST File");
      parameters = new HttpParameterCollection 
      {
        { new HttpFile("file1", "test.png", "image/png", Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAEAAAAA/CAYAAABQHc7KAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDoAABSCAABFVgAADqXAAAXb9daH5AAACLOSURBVHja5Jp3VNTXtsd/gBRFEJA2wNBhYBgcYGboQxuGNggIIlUEISgan8ZrucbEFpNAosaSGEuaqBRJ0dhNTIiioMbevVFjiSWWaK6x6+f98/glXPG1de99d6131vouBmbWcPbn7L3PPvv8BED4/ywBEAYPHvxcDRkyRKiurhYmTJggvPbaa8Ls2bOFRYsWCU1NTcKOHTuE/fv3C+fOnRNu374tnDt3Tti/f7/Q2toqtLS0CJ988okwd+5cy9deey1s4sSJhTU1Na+WlZUtKSgo+DorK+u0Xq+/rtVqL2k0mn3BwcFNfn5+s/z8/EZ4eHjobWxs3GxsbIx69+4tmJqaCqampoKRkZHwvGFsbCz07t1bMDY2Fn+3s7MTnJ2dBScnp271DwGwb98+obW11e2zzz4rr6+vb1iwYMHp2bNnP5w9ezbz589n6dKl1NfX09jYSFNTE8uXL2fx4sXU1tYyZswYcnNzUavVTx0dHX/p1avXdhMTkxmmpqYxpqampv/qAHpcuHBBf/jw4Yb29vabra2tbNu2jf3793P69GmuXbvGnTt3uH//Pk+ePOF54+HDh1y+fJm2tjbmzJlDWloatra2TwRB2C8IwjhBEFz/1QCYnDt3LvuXX37ZfuPGDc6dO8e5c+e4ceMGv/76K7/++iu3b98WXz948ICWlhamTZvG9evXaWxspK6ujtu3b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bNy4Ufj666+F9vZ2t7a2tvJTp041nD9//vTly5cfnjp1itbWVpqbm1m4cCG1tbVMmTKFP//5z4wfP56JEycyadIkXn75ZSZPnvx0woQJv4wdO3Z7dXX1jNLS0pjCwkLTzMxMISMjQ/iXBrB161Zh586dwubNm4WDBw8KBw4csDx16lTY+fPnC3/44YdXjx07tmTPnj1ff/vtt2fWrl17vbGx8fLSpUv3zZ8/v+mtt96aNXPmzBGvvPKKfty4cdLRo0cbDR06VCgqKhKKioqEfxiA/8/69wEAGjgk0I9Qd/wAAAAASUVORK5CYII=")) },
        { "dd", 20 },
        { "id", 13 },
        { new HttpFormParameter("key", "e91268b7-1162-4044-9236-7191c8c3b5d2") },
        { "contentType", "text/plain" }
      };
      result = OAuthUtility.Post("http://api.foxtools.ru/v2/Http", parameters);

      Assert.IsTrue(result.ToString().IndexOf("test.png") != -1);
      Assert.IsTrue(result.ToString().IndexOf("e91268b7-1162-4044-9236-7191c8c3b5d2") != -1);

      Console.WriteLine("OK");

      Console.WriteLine("-------------------------------------");

      Console.WriteLine("Test POST File from stream");
      using (var fs = new FileStream(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "oauth.dat"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
      {
        parameters = new HttpParameterCollection 
        {
          { new HttpFile("file1", "123.png", "image/png", fs) },
          { "dd", 20 },
          { "id", 13 },
          { "key", "e91268b7-1162-4044-9236-7191c8c3b5d2" },
          { "contentType", "text/plain" }
        };
        result = OAuthUtility.ExecuteRequest("POST", "http://api.foxtools.ru/v2/Http", parameters);

        Assert.IsTrue(result.ToString().IndexOf("123.png") != -1);

        Console.WriteLine("OK");
      }

      Console.WriteLine("-------------------------------------");

      Console.WriteLine("Test GET File");
      result = OAuthUtility.Get("https://github.com/alekseynemiro/nemiro.oauth.dll/archive/master.zip");
      if (result.IsFile)
      {
        Console.WriteLine("File: {0}", result.FileName);
        var b = (byte[])result; // test of type conversion
        Console.WriteLine("Size: {0} Kb", b.Length / 1024);
      }
      else
      {
        Console.WriteLine(result.ToString());
        Assert.Fail();
      }
      
    }
 /// <summary>
 /// Performs an async request using a <b>DELETE</b> method.
 /// </summary>
 /// <param name="endpoint">URL to which will be sent to request.</param>
 /// <param name="parameters">Parameters to be passed to request.</param>
 /// <param name="authorization">Authorization header value.</param>
 /// <param name="headers">HTTP headers for request.</param>
 /// <param name="callback">A delegate that, if provided, is called when an async request is completed.</param>
 /// <param name="accessToken">Access token to be used in the request.</param>
 /// <remarks>
 /// <para>Can not be used simultaneously <paramref name="accessToken"/> and <paramref name="authorization"/>. Use only one of these parameters.</para>
 /// </remarks>
 /// <returns>Returns an instance of the <see cref="RequestResult"/> class, which contains the result of the request.</returns>
 /// <exception cref="System.ArgumentNullException"></exception>
 /// <exception cref="RequestException"></exception>
 /// <exception cref="ArgumentException">
 /// <para>The exception occurs when the query parameters are specified at the same time <paramref name="authorization"/> and <paramref name="accessToken"/>.</para>
 /// </exception>
 public static void DeleteAsync(string endpoint = null, HttpParameterCollection parameters = null, HttpAuthorization authorization = null, NameValueCollection headers = null, AccessToken accessToken = null, ExecuteRequestAsyncCallback callback = null)
 {
   OAuthUtility.ExecuteRequestAsync("DELETE", endpoint, parameters, authorization, headers, null, accessToken, callback);
 }
Beispiel #35
0
        public void HttpParametersTest()
        {
            Console.WriteLine("Test 1");
            object obj        = null;
            string str        = null;
            int?   @int       = null;
            var    parameters = new HttpParameterCollection
            {
                { "test", obj },
                { "test2", str },
                { "test3", @int }
            };

            foreach (var p in parameters)
            {
                Assert.AreEqual(HttpParameterType.Unformed, p.ParameterType);
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }

            string r = parameters.ToStringParameters();

            Assert.AreEqual("test=&test2=&test3=", r);

            Console.WriteLine(r);

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 2");
            parameters = new HttpParameterCollection
            {
                { "n", 1 },
                { "n", 2 },
                { "n", 3 }
            };

            foreach (var p in parameters)
            {
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }

            r = parameters.ToStringParameters();

            Assert.AreEqual("n=1&n=2&n=3", r);

            Console.WriteLine(r);

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 3");
            parameters = new HttpParameterCollection
            {
                { "n[]", 1 },
                { "n[]", 2 },
                { "n[]", 3 }
            };

            foreach (var p in parameters)
            {
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }

            r = parameters.ToStringParameters();

            Assert.AreEqual("n%5b%5d=1&n%5b%5d=2&n%5b%5d=3", r);

            Console.WriteLine(r);

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 4");

            var constructorInfo = typeof(System.Web.HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First();
            var f = (System.Web.HttpPostedFile)constructorInfo.Invoke(new object[] { "oauth.png", "image/png", null });

            parameters = new HttpParameterCollection
            {
                { "file", "test.dat", "text/plain", new byte[] { 1, 2, 3, 4, 5 } },
                { "file2", "test2.dat", new byte[] { 1, 2, 3, 4, 5 } },
                { "file3", "test3.dat", new MemoryStream(Properties.Resources.oauth) },
                { "file4", f },
                { "x", 123 },
                { "y", 456 }
            };

            for (int i = 0; i < parameters.Count; i++)
            {
                var p = parameters[i];

                if (i < 3)
                {
                    Assert.AreEqual(HttpParameterType.File, p.ParameterType);
                }

                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }

            r = parameters.ToStringParameters();

            Assert.AreEqual("x=123&y=456", r);

            Console.WriteLine(r);

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 5");
            parameters = new HttpParameterCollection
            {
                { new byte[] { 1, 2, 3, 4, 5 } },
                { "x", 123 },
                { "y", 456 }
            };

            for (int i = 0; i < parameters.Count; i++)
            {
                var p = parameters[i];

                if (i == 0)
                {
                    Assert.AreEqual(HttpParameterType.RequestBody, p.ParameterType);
                }

                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }

            r = parameters.ToStringParameters();

            Assert.AreEqual("x=123&y=456", r);

            Console.WriteLine(r);

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 6");
            parameters = new HttpParameterCollection
            {
                { f },
                { "x", 123 },
                { "y", 456 }
            };

            for (int i = 0; i < parameters.Count; i++)
            {
                var p = parameters[i];

                if (i == 0)
                {
                    Assert.AreEqual(HttpParameterType.RequestBody, p.ParameterType);
                }

                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }

            r = parameters.ToStringParameters();

            Assert.AreEqual("x=123&y=456", r);

            Console.WriteLine(r);

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 7");
            parameters = new HttpParameterCollection
            {
                { Properties.Resources.oauth },
                { "x", 123 },
                { "x", 789 },
                { "y", 456 }
            };

            foreach (var p in parameters)
            {
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }

            r = parameters.ToStringParameters();

            Assert.AreEqual("x=123&x=789&y=456", r);

            Console.WriteLine(r);

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 8");
            object obj2 = Properties.Resources.oauth;

            parameters = new HttpParameterCollection
            {
                { obj2 }
            };


            for (int i = 0; i < parameters.Count; i++)
            {
                var p = parameters[i];

                if (i == 0)
                {
                    Assert.AreEqual(HttpParameterType.RequestBody, p.ParameterType);
                }

                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }

            Console.WriteLine(parameters.ToStringParameters());

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 9");
            var parameters2 = new NameValueCollection
            {
                { "a", "123" },
                { "a", "245" },
                { "b", "abc" }
            };

            foreach (var p in (HttpParameterCollection)parameters2)
            {
                Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name);
            }

            r = ((HttpParameterCollection)parameters2).ToStringParameters();

            Assert.AreEqual("a=123%2c245&b=abc", r);

            Console.WriteLine(r);
        }
Beispiel #36
0
 public static void ExecuteRequestAsync(string method = "POST", string endpoint = null, HttpParameterCollection parameters = null, HttpAuthorization authorization = null, NameValueCollection headers = null, string contentType = null, AccessToken accessToken = null, ExecuteRequestAsyncCallback callback = null, bool allowWriteStreamBuffering = false, bool allowSendChunked = true, long contentLength = -1, HttpWriteRequestStream streamWriteCallback = null, int writeBufferSize = 4096, int readBufferSize = 4096, bool donotEncodeKeys = false)
 {
     new Thread((ThreadStart)(() =>
     {
         RequestResult result;
         try
         {
             result = OAuthUtility.ExecuteRequest(method, endpoint, parameters, authorization, headers, contentType, accessToken, allowWriteStreamBuffering, allowSendChunked, contentLength, streamWriteCallback, writeBufferSize, readBufferSize, donotEncodeKeys);
         }
         catch (RequestException ex)
         {
             result = ex.RequestResult;
         }
         if (callback == null)
         {
             return;
         }
         callback(result);
     }))
     {
         IsBackground = true
     }.Start();
 }
 /// <summary>
 /// Performs a request using a <b>DELETE</b> method.
 /// </summary>
 /// <param name="endpoint">URL to which will be sent to the request.</param>
 /// <param name="parameters">Parameters to be passed to the request.</param>
 /// <param name="authorization">Authorization header value.</param>
 /// <param name="headers">HTTP headers for the request.</param>
 /// <param name="accessToken">Access token to be used in the request.</param>
 /// <returns>Returns an instance of the <see cref="RequestResult"/> class, which contains the result of the request.</returns>
 /// <remarks>
 /// <para>Can not be used simultaneously <paramref name="accessToken"/> and <paramref name="authorization"/>. Use only one of these parameters.</para>
 /// </remarks>
 /// <exception cref="System.ArgumentNullException"></exception>
 /// <exception cref="RequestException"></exception>
 /// <exception cref="ArgumentException">
 /// <para>The exception occurs when the query parameters are specified at the same time <paramref name="authorization"/> and <paramref name="accessToken"/>.</para>
 /// </exception>
 public static RequestResult Delete(string endpoint = null, HttpParameterCollection parameters = null, HttpAuthorization authorization = null, NameValueCollection headers = null, AccessToken accessToken = null)
 {
   return OAuthUtility.ExecuteRequest("DELETE", endpoint, parameters, authorization, headers, null, accessToken);
 }
 internal override void Build(string httpMethod, string url, string contentType, HttpParameterCollection parameters)
 {
   NameValueCollection param = null;
   if (parameters != null)
   {
     if (!String.IsNullOrEmpty(contentType) && contentType.ToLower().Contains("multipart/form-data"))
     {
       param = ((HttpParameterCollection)parameters.Where(itm => itm.ParameterType == HttpParameterType.Url).ToArray()).ToNameValueCollection();
     }
     else
     {
       param = parameters.ToNameValueCollection();
     }
   }
   this.Signature = new OAuthSignature
   (
     this.SignatureMethod.ToString(),
     String.Format("{0}&{1}", this.ConsumerSecret, this.TokenSecret),
     OAuthAuthorization.GetSignatureBaseString(httpMethod, url, param, this)
   ).ToString();
 }
    /// <summary>
    /// Performs a request.
    /// </summary>
    /// <param name="method">HTTP Method: <b>POST</b> (default), <b>PUT</b>, <b>GET</b> or <b>DELETE</b>.</param>
    /// <param name="endpoint">URL to which will be sent to request.</param>
    /// <param name="parameters">Parameters to be passed to request.</param>
    /// <param name="authorization">Authorization header value.</param>
    /// <param name="headers">HTTP headers for web request.</param>
    /// <param name="contentType">The value of the <b>Content-Type</b> HTTP header.</param>
    /// <param name="accessToken">Access token to be used in the request.</param>
    /// <remarks>
    /// <para>Can not be used simultaneously <paramref name="accessToken"/> and <paramref name="authorization"/>. Use only one of these parameters.</para>
    /// </remarks>
    /// <returns>Returns an instance of the <see cref="RequestResult"/> class, which contains the result of the request.</returns>
    /// <exception cref="System.ArgumentNullException"></exception>
    /// <exception cref="RequestException"></exception>
    /// <exception cref="ArgumentException">
    /// <para>The exception occurs when the query parameters are specified at the same time <paramref name="authorization"/> and <paramref name="accessToken"/>.</para>
    /// </exception>
    public static RequestResult ExecuteRequest(string method = "POST", string endpoint = null, HttpParameterCollection parameters = null, HttpAuthorization authorization = null, NameValueCollection headers = null, string contentType = null, AccessToken accessToken = null)
    {
      // checking
      if (String.IsNullOrEmpty(endpoint)) { throw new ArgumentNullException("endpoint"); }
      if (!AccessToken.IsNullOrEmpty(accessToken) && authorization != null)
      {
        throw new ArgumentException("The request can not contain both authorization headers and access token.");
      }

      // set default values
      if (!String.IsNullOrEmpty(method)) { method = method.ToUpper(); }
      string[] post = { "POST", "PUT" };
      if (String.IsNullOrEmpty(method) || (parameters != null && (parameters.HasFiles || parameters.IsRequestBody) && Array.IndexOf(post, method) == -1))
      {
        method = "POST";
      }
      bool isPost = Array.IndexOf(post, method) != -1;

      // set protocols
      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
      // ignore errors
      ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
      // --

      // access token
      if (!AccessToken.IsNullOrEmpty(accessToken))
      {
        if (accessToken.GetType() == typeof(OAuth2AccessToken) || accessToken.GetType().IsSubclassOf(typeof(OAuth2AccessToken)))
        {
          // is oauth 2.0
          var token = (OAuth2AccessToken)accessToken;
          if (!String.IsNullOrEmpty(token.TokenType) && (token.TokenType.Equals(AccessTokenType.Bearer, StringComparison.OrdinalIgnoreCase))) //  || token.TokenType.Equals(AccessTokenType.OAuth, StringComparison.OrdinalIgnoreCase)
          {
            // bearer
            authorization = new HttpAuthorization(AuthorizationType.Bearer, accessToken.Value);
          }
          else
          {
            // other
            if (parameters == null) { parameters = new HttpParameterCollection(); }
            parameters.AddUrlParameter("access_token", accessToken.Value);
          }
        }
        else if (accessToken.GetType() == typeof(OAuthAccessToken) || accessToken.GetType().IsSubclassOf(typeof(OAuthAccessToken)))
        {
          // is oauth 1.0
          authorization = new OAuthAuthorization(accessToken);
        }
        else
        {
          // I do not know that. But it's definitely need to consider.
          if (parameters == null) { parameters = new HttpParameterCollection(); }
          parameters.AddUrlParameter("access_token", accessToken.Value);
        }
      }
      // --

      string requestUrl = endpoint; // need source endpoint for signature

      if (!isPost && parameters != null && parameters.Count > 0)
      {
        // set parameters to the URL if the request is executed using the GET method
        requestUrl += (requestUrl.Contains("?") ? "&" : "?");
        requestUrl += parameters.ToStringParameters();
      }
      else if (isPost && parameters != null && parameters.Count > 0)
      {
        // is POST or PUT method
        if (parameters.IsRequestBody)
        {
          // all parameters to url
          HttpParameterCollection p = parameters.Where(itm => itm.ParameterType != HttpParameterType.RequestBody).ToArray();
          if (p.Count > 0)
          {
            requestUrl += (requestUrl.Contains("?") ? "&" : "?");
            requestUrl += p.ToStringParameters();
          }
        }
        else
        {
          // url parameters to endpoint
          HttpParameterCollection p = parameters.Where(itm => itm.ParameterType == HttpParameterType.Url).ToArray();
          if (p.Count > 0)
          {
            requestUrl += (requestUrl.Contains("?") ? "&" : "?");
            requestUrl += p.ToStringParameters();
          }
        }
      }

      // create request
      var req = (HttpWebRequest)HttpWebRequest.Create(requestUrl);

      // http method
      req.Method = method;

      // req.ProtocolVersion = HttpVersion.Version10;

      // user-agent (required for some providers)
      req.UserAgent = "Nemiro.OAuth";

      // json format acceptable for the response
      req.Accept = "application/json";

      // set parameters to the body if the request is executed using the POST method
      if (isPost)
      {
        if (parameters != null && parameters.Count > 0)
        {
          req.ContentType = contentType;
          parameters.WriteToRequestStream(req);
        }
        else
        {
          // for some servers
          req.ContentLength = 0;
        }
      }

      // set authorization header
      if (authorization != null)
      {
        // build authorization
        authorization.Build(method, endpoint, req.ContentType, parameters);
        // add authorization to headers
        if (headers == null) { headers = new NameValueCollection(); }
        if (String.IsNullOrEmpty(headers["Authorization"]))
        {
          headers.Add("Authorization", authorization.ToString());
        }
        else
        {
          headers["Authorization"] = authorization.ToString();
        }
      }

      // headers
      if (headers != null)
      {
        req.Headers.Add(headers);
      }

      WebHeaderCollection rh = null;
      Exception exception = null;
      string ct = "";
      int status = 0;
      byte[] result;

      try
      {
        // executes the request
        using (var resp = (HttpWebResponse)req.GetResponse())
        {
          ct = resp.ContentType;
          rh = resp.Headers;
          status = (int)resp.StatusCode;
          result = OAuthUtility.ReadResponseStream(resp);
        }
      }
      catch (WebException ex)
      { // web exception, 
        if (ex.Response != null)
        { // reading contents of the response
          using (var resp = (HttpWebResponse)ex.Response)
          {
            ct = resp.ContentType;
            rh = resp.Headers;
            status = (int)resp.StatusCode;
            result = OAuthUtility.ReadResponseStream(resp);
          }
        }
        else
        { // no response, get error message
          result = Encoding.UTF8.GetBytes(ex.Message);
        }
        exception = ex;
      }
      catch (Exception ex)
      { // other exception
        result = Encoding.UTF8.GetBytes(ex.Message);
        exception = ex;
      }

      // exception
      if (exception != null)
      {
        throw new RequestException(ct, result, exception, rh, status);
      }

      // result
      return new RequestResult(ct, result, rh, status);
    }