Advertisement
4535992

Untitled

Jan 27th, 2017
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.30 KB | None | 0 0
  1.  
  2.         public bool LoginByAutorizationCode(out string result, string url, string clientId = null, string grantType = "authorization_code")
  3.         {
  4.             try
  5.             {
  6.                 NameValueCollection queryParams = HttpUtility.ParseQueryString(new UriBuilder(url).Query);
  7.                 CODE = queryParams["code"];
  8.                 if (string.IsNullOrEmpty(CODE)) CODE = HttpContext.Current.Request.QueryString["code"];
  9.                 HttpContext.Current.Session["CODE"] = CODE;
  10.  
  11.                 Dictionary<string, object> fields = new Dictionary<string, object>
  12.                 {
  13.                     {"grant_type", grantType},
  14.                     {"client_id", HttpUtility.UrlEncode(CLIENT_ID)},
  15.                     {"client_secret", HttpUtility.UrlEncode(CLIENT_SECRET)},
  16.                     {"redirect_uri", REDIRECT_URI},
  17.                     {"code", CODE}
  18.                 };
  19.                 KeyValuePair<string, string> authorizations = new KeyValuePair<string, string>
  20.                     ("Authorization", "Bearer " + CODE + "" );
  21.                 //Net45
  22.                 result = Post(ACCESS_TOKEN_URL, fields).Result;//Send POST and get result
  23.                
  24.                 Dictionary<string, string> headers = new Dictionary<string, string>()
  25.                 {
  26.                     { "Bearer", CODE }
  27.                 };
  28.                 // The response body of this request contains the `access_token` necessary to authenticate your requests
  29.                 // Ex : {"access_token": "1234", "token_type": "Bearer", "expires_in": 36000, "refresh_token": "5678", "scope": "read write"}
  30.                 // - expires_in => seconds to live for this `access_token`
  31.                 // - refresh_token => A token used to fetch a new `access_token` (See below)
  32.  
  33.                 // Now you're all set, the following request shows how to use your `access_token` in your requests
  34.                 // If your access token is recognized, this will return information regarding the current user
  35.                 //result = Get(USER_URL, null, headers).Result;
  36.                 return true;
  37.             }
  38.             catch (Exception ex)
  39.             {
  40.                 result = ("[" + new StackTrace(ex, true).GetFrame(0).GetFileLineNumber() + "][" + new StackTrace(ex, true).GetFrame(0).GetMethod().Name + "] " + ex.Message + " => " + ex.InnerException);
  41.                 return false;
  42.             }
  43.  
  44.         }
  45.  
  46.   public async Task<string> Post(string url, IEnumerable<KeyValuePair<string, object>> parameters, IEnumerable<KeyValuePair<string, string>> headers = null, KeyValuePair<string, string> authorization = default(KeyValuePair<string, string>))
  47.         {
  48.             using (var client = new HttpClient())
  49.             {
  50.                 if (!authorization.Equals(default(KeyValuePair<string, string>))) client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authorization.Key, authorization.Value); //"Bearer", "Your Oauth token"
  51.                 if (headers != null) foreach (KeyValuePair<string, string> pair in headers) client.DefaultRequestHeaders.Add(pair.Key, pair.Value);
  52.                 using (HttpContent formRequestContent = new FormUrlEncodedContent(parameters.ToDictionary(k => k.Key, k => k.Value?.ToString() ?? "")))
  53.                 using (HttpResponseMessage response = await client.PostAsync(url, formRequestContent))
  54.                 {
  55.                    
  56.                     //HttpResponseMessage res = await client.PostAsync(url, formRequestContent);
  57.                     //string test = res.Content.ReadAsStringAsync().Result;
  58.                     if (response.IsSuccessStatusCode)
  59.                     {
  60.                         using (HttpContent responseContent = response.Content)
  61.                         {
  62.                            
  63.                             var responseString = await responseContent.ReadAsStringAsync();
  64.                             if (responseString != null) return responseString;
  65.                             return "The POST CALL HAS SUCCEDED BUT RETURN NULL => " + response.RequestMessage;
  66.                         }
  67.                     }
  68.                     else
  69.                     {
  70.                         return "The POST CALL HAS NOT SUCCEDED  => STATUS CODE = " + response.StatusCode;
  71.                     }
  72.                 }
  73.             }
  74.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement