Google的服务帐户OAuth2在C#.NET中用于URL Shortener API

Mad*_*ddy 5 c# asp.net google-api oauth-2.0 google-oauth

我已经浪费了一整天的时间来在C#.NET中为URL Shortener API查找Google的服务帐户OAuth2的示例代码.

我正在尝试使用缩短的api与服务器到服务器请求.

请帮我.

谢谢

I4V*_*I4V 7

使用Json.Net库(您可以从这里获取API密钥)

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"];
Run Code Online (Sandbox Code Playgroud)