我已经浪费了一整天的时间来在C#.NET中为Google Shortener API找到Google的服务帐户OAuth2的示例代码。
我正在尝试使用缩短的api与服务器到服务器请求。
请帮帮我。
由于
答案 0 :(得分:7)
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"];
答案 1 :(得分:0)
在我看来,你应该阅读正确的Google page。
部分内容:
您的应用程序发送给Google URL Shortener API的每个请求 需要向Google确定您的申请。有两种方法可以 识别您的应用程序:使用OAuth 2.0令牌(也是 授权请求)和/或使用应用程序的API密钥。
获取和使用API密钥
必须为Google URL Shortener API提供公共数据请求 附带标识符,可以是API密钥或身份验证 令牌。
要获取API密钥,请访问API控制台。在“服务”窗格中, 激活Google URL Shortener API;如果出现服务条款, 阅读并接受它们。
接下来,转到API Access窗格。 API密钥位于底部附近 该窗格在标题为“Simple API Access。”的部分中。
拥有API密钥后,您的应用程序可以附加查询 所有请求网址的参数
key=yourAPIKey
。API密钥可以安全地嵌入到URL中;它不需要任何 编码
缩短长网
Google URL Shortener API可让您像对方一样缩短网址 goo.gl.例如,缩短URL http://www.google.com/,发送以下请求:
POST https://www.googleapis.com/urlshortener/v1/url
Content-Type: application/json
{"longUrl": "http://www.google.com/"}
答案 2 :(得分:0)
我使用HttpClient在Windows 8中使用它。这是代码,不需要Api密钥。
var serializedUrl = JsonConvert.SerializeObject(new { longUrl = yourlongUrl});
HttpClient client = new HttpClient();
var Content = new StringContent(serializedUrl, Encoding.UTF8, "application/json");
var resp = await client.PostAsync("https://www.googleapis.com/urlshortener/v1/url", Content);
var content = await resp.Content.ReadAsStringAsync();
var jsonObject = JsonConvert.DeserializeObject<JObject>(content);
var shortedUrl = jsonObject["id"].Value<string>();
答案 3 :(得分:0)
以下代码对我有用。此代码建立服务器到服务器连接并获取身份验证令牌。然后它会调用缩短URL。 API密钥存储在app.config中。
您可以在此处详细了解:http://www.am22tech.com/google-url-shortener-api-shorten-url/
using System;
using System.Collections.Generic;
using System.Web;
using System.Configuration;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Urlshortener.v1;
using Google.Apis.Urlshortener.v1.Data;
using Google.Apis.Services;
public static string shortenURL(string urlToShorten, string webSiteBasePath)
{
string shortURL = string.Empty;
try
{
/********************************************************************/
string AuthenticationToken = string.Empty;
var certificate = new X509Certificate2(webSiteBasePath + "/" + ConfigurationManager.AppSettings["IHSGoogleURlShortenerPrivateKeyName"].ToString(),
ConfigurationManager.AppSettings["IHSGoogleURlShortenerPrivateKeySecret"].ToString(),
X509KeyStorageFlags.MachineKeySet |
X509KeyStorageFlags.PersistKeySet |
X509KeyStorageFlags.Exportable);
String serviceAccountEmail = ConfigurationManager.AppSettings["IHSGoogleURLShortenerServiceAcEmail"].ToString();
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { UrlshortenerService.Scope.Urlshortener }
}.FromCertificate(certificate));
if (credential.RequestAccessTokenAsync(CancellationToken.None).Result)
{
AuthenticationToken = credential.Token.AccessToken;
}
// Create the service.
var service = new UrlshortenerService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ConfigurationManager.AppSettings["IHSGoogleURLShortnerAppName"].ToString(),
});
// Shorten URL
Url toInsert = new Url { LongUrl = urlToShorten };
toInsert = service.Url.Insert(toInsert).Execute();
shortURL = toInsert.Id;
}
return (shortURL);
}