Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public bool LoginByAutorizationCode(out string result, string url, string clientId = null, string grantType = "authorization_code")
- {
- try
- {
- NameValueCollection queryParams = HttpUtility.ParseQueryString(new UriBuilder(url).Query);
- CODE = queryParams["code"];
- if (string.IsNullOrEmpty(CODE)) CODE = HttpContext.Current.Request.QueryString["code"];
- HttpContext.Current.Session["CODE"] = CODE;
- Dictionary<string, object> fields = new Dictionary<string, object>
- {
- {"grant_type", grantType},
- {"client_id", HttpUtility.UrlEncode(CLIENT_ID)},
- {"client_secret", HttpUtility.UrlEncode(CLIENT_SECRET)},
- {"redirect_uri", REDIRECT_URI},
- {"code", CODE}
- };
- KeyValuePair<string, string> authorizations = new KeyValuePair<string, string>
- ("Authorization", "Bearer " + CODE + "" );
- //Net45
- result = Post(ACCESS_TOKEN_URL, fields).Result;//Send POST and get result
- Dictionary<string, string> headers = new Dictionary<string, string>()
- {
- { "Bearer", CODE }
- };
- // The response body of this request contains the `access_token` necessary to authenticate your requests
- // Ex : {"access_token": "1234", "token_type": "Bearer", "expires_in": 36000, "refresh_token": "5678", "scope": "read write"}
- // - expires_in => seconds to live for this `access_token`
- // - refresh_token => A token used to fetch a new `access_token` (See below)
- // Now you're all set, the following request shows how to use your `access_token` in your requests
- // If your access token is recognized, this will return information regarding the current user
- //result = Get(USER_URL, null, headers).Result;
- return true;
- }
- catch (Exception ex)
- {
- result = ("[" + new StackTrace(ex, true).GetFrame(0).GetFileLineNumber() + "][" + new StackTrace(ex, true).GetFrame(0).GetMethod().Name + "] " + ex.Message + " => " + ex.InnerException);
- return false;
- }
- }
- 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>))
- {
- using (var client = new HttpClient())
- {
- if (!authorization.Equals(default(KeyValuePair<string, string>))) client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authorization.Key, authorization.Value); //"Bearer", "Your Oauth token"
- if (headers != null) foreach (KeyValuePair<string, string> pair in headers) client.DefaultRequestHeaders.Add(pair.Key, pair.Value);
- using (HttpContent formRequestContent = new FormUrlEncodedContent(parameters.ToDictionary(k => k.Key, k => k.Value?.ToString() ?? "")))
- using (HttpResponseMessage response = await client.PostAsync(url, formRequestContent))
- {
- //HttpResponseMessage res = await client.PostAsync(url, formRequestContent);
- //string test = res.Content.ReadAsStringAsync().Result;
- if (response.IsSuccessStatusCode)
- {
- using (HttpContent responseContent = response.Content)
- {
- var responseString = await responseContent.ReadAsStringAsync();
- if (responseString != null) return responseString;
- return "The POST CALL HAS SUCCEDED BUT RETURN NULL => " + response.RequestMessage;
- }
- }
- else
- {
- return "The POST CALL HAS NOT SUCCEDED => STATUS CODE = " + response.StatusCode;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement