Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google's Service Account OAuth2 in C#.NET for URL Shortener API

I already wasted my full day for finding sample code for Google's Service Account OAuth2 in C#.NET for URL Shortener API.

I am trying to use shortener api with server to server request.

Please help me.

Thanks

like image 833
Maddy Avatar asked Mar 22 '23 15:03

Maddy


1 Answers

Using Json.Net library (you can get the API key from here)

string longURL="http://www.google.com";

string url = "https://www.googleapis.com/urlshortener/v1/url?key=" + apiKey;
WebClient client = new WebClient();
client.Headers["Content-Type"] = "application/json";
var response = client.UploadString(url,JsonConvert.SerializeObject(new { longUrl = longURL }));

var shortUrl = (string)JObject.Parse(response)["id"];
like image 65
I4V Avatar answered Apr 05 '23 23:04

I4V