Google Url Shortener with C#

Create a public API key.  If you dont know how read this. Google Developer Console Create Public API Key

Create a new project in visual studio and install the following NuGet package. Install-Package Google.Apis.Urlshortener.v1


 

public HttpResponseMessage GetKargoUrl(URL_IN param)
{
if (param.P_URL_TYPE == “1”)
{
UrlshortenerService service = new UrlshortenerService(new BaseClientService.Initializer()
{
ApiKey = “ApiKey”,
ApplicationName = “ProjectName”
});
var m = new Google.Apis.Urlshortener.v1.Data.Url();
m.LongUrl = param.P_URL;

return Request.CreateResponse(HttpStatusCode.OK, service.Url.Insert(m).Execute().Id);
}
else
{
UrlshortenerService service = new UrlshortenerService(new BaseClientService.Initializer() {
ApiKey = “ApiKey”,
ApplicationName = “ProjectName”
});
return Request.CreateResponse(HttpStatusCode.OK, service.Url.Get(param.P_URL).Execute().LongUrl);
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: