public static ResponseStatus ToNGitHubResponseStatus(this RestSharp.ResponseStatus status) { switch (status) { case RestSharp.ResponseStatus.Completed: return(ResponseStatus.Completed); case RestSharp.ResponseStatus.Error: return(ResponseStatus.Error); case RestSharp.ResponseStatus.None: return(ResponseStatus.None); case RestSharp.ResponseStatus.TimedOut: return(ResponseStatus.TimedOut); case RestSharp.ResponseStatus.Aborted: return(ResponseStatus.Aborted); default: throw new InvalidOperationException(); } }
string GetToken() { var requestUrl = "https://www.reddit.com/api/v1/access_token"; RestSharp.RestClient rc = new RestSharp.RestClient(); RestSharp.RestRequest request = new RestSharp.RestRequest(requestUrl, RestSharp.Method.POST); var appId = Configuration["RedditSecretBot:AppId"]; var Secret = Configuration["RedditSecretBot:Secret"]; request.AddHeader("Authorization", "Basic " + Base64Encode(appId + ":" + Secret)); request.AddParameter("grant_type", "client_credentials"); RestSharp.RestResponse restResponse = (RestSharp.RestResponse)rc.Execute(request); RestSharp.ResponseStatus responseStatus = restResponse.ResponseStatus; if (responseStatus == RestSharp.ResponseStatus.Completed) { var response = restResponse.Content.ToString(); var convertedResponse = JsonConvert.DeserializeObject <TokenResponse>(response); return(convertedResponse.access_token); } return(String.Empty); // The response from this request, //if successful, will be JSON of the following format: //{ // "access_token": Your access token, // "token_type": "bearer", // "expires_in": Unix Epoch Seconds, // "scope": A scope string, //} }