我已經浪費了整整一天的時間在C#.NET中爲Google Shortener API查找Google服務帳戶OAuth2的示例代碼。Google的服務帳戶OAuth2在C#.NET中用於URL縮短器API
我想用服務器到服務器請求使用更短的api。
請幫幫我。
感謝
我已經浪費了整整一天的時間在C#.NET中爲Google Shortener API查找Google服務帳戶OAuth2的示例代碼。Google的服務帳戶OAuth2在C#.NET中用於URL縮短器API
我想用服務器到服務器請求使用更短的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"];
在我看來,你應該閱讀適當Google page。
它的某些部分:
每個請求您的應用程序發送給谷歌URL縮短API 需要確定您的應用程序谷歌。 有兩種方式可以識別您的應用程序:使用OAuth 2.0令牌(也可以 授權請求)和/或使用應用程序的API密鑰。
獲取和使用密鑰
請求到谷歌URL縮短API用於公共數據的API必須 伴隨的標識符,其可以是API密鑰或一個auth 令牌。
要獲取API密鑰,請訪問API控制檯。在服務窗格中, 激活Google URL Shortener API;如果出現服務條款, 會閱讀並接受它們。
接下來,轉到API訪問面板。該API密鑰位於該窗格的底部,即標題爲「簡單API訪問」的部分。
在您擁有API密鑰後,您的應用程序可以將查詢 參數
key=yourAPIKey
附加到所有請求URL。API密鑰對於嵌入到URL中是安全的;它不需要任何 編碼。
縮短長的URL
的谷歌URL縮短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/"}
我把它工作在Windows 8與HttpClient。這裏是代碼,不需要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>();
這是我工作的代碼。此代碼建立服務器到服務器的連接並獲取身份驗證令牌。然後它會打電話縮短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);
}
謝謝回覆馬可。我在同一頁面上發現了此注意事項注意:強烈建議您使用API密鑰。 身份驗證令牌對於縮短請求是可選的。如果您提供一個,那麼短網址將是唯一的,並且會在goo.gl的經過身份驗證的用戶儀表板中顯示。 – Maddy
是否意味着如果我只使用API密鑰,短網址不會是唯一的? – Maddy