public T?ProcessActionResult(string?responseJson, Enum theAction) { var accAct = new AccountActivity(); if ((AccountActivityType)theAction == AccountActivityType.Webhooks) { accAct.WebhooksValue = new WebhooksValue { Webhooks = new Webhook?[] { string.IsNullOrWhiteSpace(responseJson) ? new Webhook() : JsonSerializer.Deserialize <Webhook>(responseJson) } } } ; else if ((AccountActivityType)theAction == AccountActivityType.Subscriptions) { accAct.SubscriptionValue = new SubscriptionValue { IsSubscribed = true } } ; return(accAct.ItemCast(default(T))); } }
/// <summary> /// Deletes a new webhook to account /// </summary> /// <param name="webhookID">Url of webhook.</param> /// <returns>Account Activity data.</returns> public async Task <AccountActivity?> DeleteAccountActivityWebhookAsync(ulong webhookID, CancellationToken cancelToken = default) { if (webhookID == default) { throw new ArgumentException($"{nameof(webhookID)} must be set.", nameof(webhookID)); } var newUrl = BaseUrl + $"account_activity/webhooks/{webhookID}.json"; var accActValue = new AccountActivity { WebhookID = webhookID }; RawResult = await TwitterExecutor.SendJsonToTwitterAsync( HttpMethod.Delete.ToString(), newUrl, new Dictionary <string, string>(), accActValue, cancelToken) .ConfigureAwait(false); var reqProc = new AccountActivityRequestProcessor <AccountActivity>(); AccountActivity?accAct = reqProc.ProcessActionResult(RawResult, AccountActivityType.Webhooks); if (accAct != null) { accAct.WebhookID = webhookID; } return(accAct); }
/// <summary> /// Adds a new webhook to account /// </summary> /// <param name="url">Url of webhook.</param> /// <returns>Account Activity data.</returns> public async Task <AccountActivity> AddAccountActivityWebhookAsync(string url, CancellationToken cancelToken = default(CancellationToken)) { if (string.IsNullOrWhiteSpace(url)) { throw new ArgumentException($"{nameof(url)} must be set.", nameof(url)); } var newUrl = BaseUrl + $"account_activity/webhooks.json"; RawResult = await TwitterExecutor.PostFormUrlEncodedToTwitterAsync <AccountActivity>( HttpMethod.Post.ToString(), newUrl, new Dictionary <string, string> { { "url", url } }, cancelToken) .ConfigureAwait(false); var reqProc = new AccountActivityRequestProcessor <AccountActivity>(); AccountActivity accAct = reqProc.ProcessActionResult(RawResult, AccountActivityType.Webhooks); accAct.Url = url; return(accAct); }
/// <summary> /// Adds a user subscription to specified webhook /// </summary> /// <param name="webhookID">ID of webhook user is subscribing to.</param> /// <returns>Account Activity data.</returns> /// <exception cref="TwitterQueryException"> /// Throws TwitterQueryException when an AddAccountActivitySubscriptionAsync fails. /// </exception> public async Task <AccountActivity> AddAccountActivitySubscriptionAsync(ulong webhookID, CancellationToken cancelToken = default(CancellationToken)) { if (webhookID == default(ulong)) { throw new ArgumentException($"{nameof(webhookID)} must be set.", nameof(webhookID)); } var newUrl = BaseUrl + $"account_activity/webhooks/{webhookID}/subscriptions.json"; var accActValue = new AccountActivityValue(); RawResult = await TwitterExecutor.SendJsonToTwitterAsync( HttpMethod.Post.ToString(), newUrl, new Dictionary <string, string>(), accActValue, cancelToken) .ConfigureAwait(false); var reqProc = new AccountActivityRequestProcessor <AccountActivity>(); AccountActivity accAct = reqProc.ProcessActionResult(RawResult, AccountActivityType.Subscriptions); accAct.WebhookID = webhookID; return(accAct); }
public T ProcessActionResult(string responseJson, Enum theAction) { var accAct = new AccountActivity(); if ((AccountActivityType)theAction == AccountActivityType.Webhooks) { accAct.WebhooksValue = new WebhooksValue { Webhooks = new Webhook[] { responseJson == null ? new Webhook() : JsonConvert.DeserializeObject <Webhook>(responseJson) } } } ; else if ((AccountActivityType)theAction == AccountActivityType.Subscriptions) { accAct.SubscriptionValue = new SubscriptionValue { IsSubscribed = true } } ; return(accAct.ItemCast(default(T))); } }
public static bool IsValidPostSignature(this AccountActivity accAct, HttpRequestMessage request, string message, string consumerSecret) { string webhooksSignature = request?.Headers ?.GetValues("x-twitter-webhooks-signature") ?.First() ?.Replace("sha256=", ""); if (webhooksSignature == null) { return(false); } byte[] webhookSignatureByes = Convert.FromBase64String(webhooksSignature); byte[] keyBytes = Encoding.UTF8.GetBytes(consumerSecret); byte[] contentBytes = Encoding.UTF8.GetBytes(message); var hmac = new HMACSHA256(keyBytes); var contentHash = hmac.ComputeHash(contentBytes); if (!SecureCompareEqual(webhookSignatureByes, contentHash)) { return(false); } return(true); }
public static string BuildCrcResponse(this AccountActivity accAct, string crc_token, string consumerSecret) { byte[] keyBytes = Encoding.UTF8.GetBytes(consumerSecret); byte[] crcBytes = Encoding.UTF8.GetBytes(crc_token); var hmac = new HMACSHA256(keyBytes); var hash = hmac.ComputeHash(crcBytes); var base64Hmac = Convert.ToBase64String(hash); return("sha256=" + base64Hmac); }