/// <summary> /// Gets the OAuth token for the user associated with the provided code. /// </summary> /// <param name="code">Sent by reddit as a parameter in the return uri.</param> /// <param name="isRefresh">Set to true for refresh requests.</param> /// <returns></returns> public async Task <string> GetOAuthTokenAsync(string code, bool isRefresh = false) { var json = await _webAgent.ExecuteRequestAsync(() => { var request = _webAgent.CreateRequest(AccessUrl, "POST"); request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(_clientId + ":" + _clientSecret))); if (isRefresh) { _webAgent.WritePostBody(request, new { grant_type = "refresh_token", refresh_token = code }); } else { _webAgent.WritePostBody(request, new { grant_type = "authorization_code", code, redirect_uri = _redirectUri }); } return(request); }).ConfigureAwait(false); return(json["access_token"].ToString()); throw new AuthenticationException(json.ToString()); }
/// <summary> /// Gets the OAuth token for the user associated with the provided code. /// </summary> /// <param name="code">Sent by reddit as a parameter in the return uri.</param> /// <param name="isRefresh">Set to true for refresh requests.</param> /// <returns></returns> public async Task <string> GetOAuthTokenAsync(string code, bool isRefresh = false) { //TODO test mono and make sure this works without security issues. Shouldn't be handled by library, should require install of cert or at least explicit calls to ingore certs //if (Type.GetType("Mono.Runtime") != null) // ServicePointManager.ServerCertificateValidationCallback = (s, c, ch, ssl) => true; var json = await _webAgent.ExecuteRequestAsync(() => { var request = _webAgent.CreateRequest(AccessUrl, "POST"); request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(_clientId + ":" + _clientSecret))); if (isRefresh) { _webAgent.WritePostBody(request, new { grant_type = "refresh_token", refresh_token = code }); } else { _webAgent.WritePostBody(request, new { grant_type = "authorization_code", code, redirect_uri = _redirectUri }); } return(request); }).ConfigureAwait(false); if (json["access_token"] != null) { return(json["access_token"].ToString()); } throw new AuthenticationException("Could not log in."); }
/// <summary> /// Logs in the current Reddit instance. /// </summary> /// <param name="username">The username of the user to log on to.</param> /// <param name="password">The password of the user to log on to.</param> /// <param name="useSsl">Whether to use SSL or not. (default: true)</param> /// <returns></returns> public AuthenticatedUser LogIn(string username, string password, bool useSsl = true) { if (Type.GetType("Mono.Runtime") != null) { ServicePointManager.ServerCertificateValidationCallback = (s, c, ch, ssl) => true; } _webAgent.Cookies = new CookieContainer(); HttpWebRequest request; if (useSsl) { request = _webAgent.CreatePost(SslLoginUrl); } else { request = _webAgent.CreatePost(LoginUrl); } var stream = request.GetRequestStream(); if (useSsl) { _webAgent.WritePostBody(stream, new { user = username, passwd = password, api_type = "json" }); } else { _webAgent.WritePostBody(stream, new { user = username, passwd = password, api_type = "json", op = "login" }); } stream.Close(); var response = (HttpWebResponse)request.GetResponse(); var result = _webAgent.GetResponseString(response.GetResponseStream()); var json = JObject.Parse(result)["json"]; if (json["errors"].Count() != 0) { throw new AuthenticationException("Incorrect login."); } InitOrUpdateUser(); return(User); }
/// <summary> /// Gets the OAuth token for the user associated with the provided code. /// </summary> /// <param name="code">Sent by reddit as a parameter in the return uri.</param> /// <param name="isRefresh">Set to true for refresh requests.</param> /// <returns></returns> public string GetOAuthToken(string code, bool isRefresh = false) { if (Type.GetType("Mono.Runtime") != null) { ServicePointManager.ServerCertificateValidationCallback = (s, c, ch, ssl) => true; } _webAgent.Cookies = new CookieContainer(); var request = _webAgent.CreatePost(AccessUrl); request.InitWebReqProxy(); request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(_clientId + ":" + _clientSecret)); var stream = request.GetRequestStream(); if (isRefresh) { _webAgent.WritePostBody(stream, new { grant_type = "refresh_token", refresh_token = code }); } else { _webAgent.WritePostBody(stream, new { grant_type = "authorization_code", code, redirect_uri = _redirectUri }); } stream.Close(); var json = _webAgent.ExecuteRequest(request); if (json["access_token"] != null) { if (json["refresh_token"] != null) { RefreshToken = json["refresh_token"].ToString(); } OAuthToken = json["access_token"].ToString(); return(json["access_token"].ToString()); } throw new AuthenticationException("Could not log in."); }