Last active
January 22, 2018 04:15
-
-
Save ryanswanstrom/476c7d5b464d5ce41c51ff691ac87a37 to your computer and use it in GitHub Desktop.
How to setup Pocket for API calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace URLCall | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Getting started"); | |
/*var consumer_key = ""; | |
var request_tk = ""; | |
HttpClient client = new HttpClient(); | |
var values = new Dictionary<string, string> | |
{ | |
{ "consumer_key", consumer_key }, | |
{ "redirect_uri", "NewsScraper:authorizationFinished" } | |
}; | |
var content = new FormUrlEncodedContent(values); | |
var response = client.PostAsync("https://getpocket.com/v3/oauth/request", content).Result; | |
if (response.IsSuccessStatusCode) | |
{ | |
var responseContent = response.Content; | |
// by calling .Result you are synchronously reading the result | |
var responseString = responseContent.ReadAsStringAsync().Result; | |
request_tk = responseString.Substring(5); | |
var url_callback = "https://infocollection.azurewebsites.net/api/HttpTriggerCSharp1?code=COHrQvNMNEDkhm2TXe7Pv6y7I3eBK9jvVEF947WhsdN7a1wVvdGesQ=="; | |
Console.WriteLine($"https://getpocket.com/auth/authorize?request_token={request_tk}&redirect_uri={url_callback}"); | |
// | |
} | |
Console.ReadLine(); | |
// convert the code to an access token | |
values = new Dictionary<string, string> | |
{ | |
{ "consumer_key", consumer_key }, | |
{ "code", request_tk } | |
}; | |
content = new FormUrlEncodedContent(values); | |
response = client.PostAsync("https://getpocket.com/v3/oauth/authorize", content).Result; | |
if (response.IsSuccessStatusCode) | |
{ | |
var responseContent = response.Content; | |
// by calling .Result you are synchronously reading the result | |
var responseString = responseContent.ReadAsStringAsync().Result; | |
Console.WriteLine(responseString); | |
} else | |
{ | |
Console.WriteLine("Cannot get access token"); | |
Console.WriteLine(response.RequestMessage.ToString()); | |
} | |
*/ | |
addUrl(); | |
Console.ReadLine(); | |
} | |
static void addUrl() | |
{ | |
Console.WriteLine("adding URL"); | |
HttpClient client = new HttpClient(); | |
var values = new Dictionary<string, string> | |
{ | |
{ "consumer_key", "" }, | |
{ "access_token", "" }, | |
{ "url", "https://hbr.org/2018/01/machine-learning-can-help-b2b-firms-learn-more-about-their-customers" }, | |
{ "tags", "datascience,machinelearning,AI" } | |
}; | |
var content = new FormUrlEncodedContent(values); | |
var response = client.PostAsync("https://getpocket.com/v3/add", content).Result; | |
if (response.IsSuccessStatusCode) | |
{ | |
Console.WriteLine("successfully adding URL"); | |
var responseContent = response.Content; | |
// by calling .Result you are synchronously reading the result | |
var responseString = responseContent.ReadAsStringAsync().Result; | |
Console.WriteLine(responseString); | |
} else | |
{ | |
Console.WriteLine("Cannot add URL"); | |
} | |
Console.WriteLine("finished adding URL"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment