public static async Task<bool> StartApplicationOnlyAuth(this IApplicationSession appsession)
{
if (string.IsNullOrEmpty(appsession.clientID))
throw new ArgumentException("Twitter Consumer Key is required for Application only Auth");
if (string.IsNullOrEmpty(appsession.clientSecret))
throw new ArgumentException("Twitter Consumer Secret is required for Application only Auth");
// ref: https://dev.twitter.com/docs/auth/application-only-auth
// and ref: http://tools.ietf.org/html/rfc6749#section-4.4
var oAuth2TokenUrlPostRequestRfc6749 = new SortedDictionary<string, string>
{
{"grant_type", "client_credentials"}
};
var result = await appsession.PostAsync(TwitterApi.OAuth2TokenUrl(), oAuth2TokenUrlPostRequestRfc6749, forInitialAuth: true);
if (!result.IsSuccessStatusCode)
return false;
var content = await result.Content.ReadAsStringAsync();
var jresponse = JObject.Parse(content);
appsession.bearerToken = (string) jresponse["access_token"];
appsession.IsActive = true;
return true;
}