當前位置: 首頁>>代碼示例>>C#>>正文


C# AccessToken.GetType方法代碼示例

本文整理匯總了C#中AccessToken.GetType方法的典型用法代碼示例。如果您正苦於以下問題:C# AccessToken.GetType方法的具體用法?C# AccessToken.GetType怎麽用?C# AccessToken.GetType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在AccessToken的用法示例。


在下文中一共展示了AccessToken.GetType方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ExecuteRequest

    /// <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
//.........這裏部分代碼省略.........
開發者ID:DreamerUA,項目名稱:nemiro.oauth.dll,代碼行數:101,代碼來源:OAuthUtility.cs


注:本文中的AccessToken.GetType方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。