프로그램

google url-shortenerl api 사용하기

mulderu 2013. 5. 9. 18:42

요즘 짧은 url을 이용하는 경우가 많아 졌습니다. (url-shortener)

보통 브라우저 플러그인을 많이 이용하여 만드는데, 프로그램적으로 접근도 가능 합니다.

아래와 같이 사용해 보세요.

 아래는 curl 을 이용하여 google url shorten document url 을 축약하고 원복하는 예제 입니다.

 https://developers.google.com/url-shortener/v1/getting_started?hl=ko


********** url 을 축약 하기.
origianl url : https://developers.google.com/url-shortener/v1/getting_started?hl=ko

downloads$ curl -i -v -X POST https://www.googleapis.com/urlshortener/v1/url \
>   -H 'Content-Type: application/json' \
>   -d '{"longUrl": "https://developers.google.com/url-shortener/v1/getting_started?hl=ko"}'
* About to connect() to www.googleapis.com port 443 (#0)
*   Trying 74.125.31.95...
* connected
* Connected to www.googleapis.com (74.125.31.95) port 443 (#0)
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using RC4-SHA
* Server certificate:
* 	 subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.googleapis.com
* 	 start date: 2013-04-24 15:08:20 GMT
* 	 expire date: 2013-12-30 15:08:20 GMT
* 	 subjectAltName: www.googleapis.com matched
* 	 issuer: C=US; O=Google Inc; CN=Google Internet Authority
* 	 SSL certificate verify ok.
> POST /urlshortener/v1/url HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin10.8.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5 libidn/1.17
> Host: www.googleapis.com
> Accept: */*
> Content-Type: application/json
> Content-Length: 83
> 
* upload completely sent off: 83 out of 83 bytes
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
Pragma: no-cache
< Expires: Fri, 01 Jan 1990 00:00:00 GMT
Expires: Fri, 01 Jan 1990 00:00:00 GMT
< Date: Thu, 09 May 2013 09:36:50 GMT
Date: Thu, 09 May 2013 09:36:50 GMT
< ETag: "1Q2LP9og6bfH7yqsv2dGqHDwZTM/tMcjS2bWvPvp3Y1KEW-FeBL_yQs"
ETag: "1Q2LP9og6bfH7yqsv2dGqHDwZTM/tMcjS2bWvPvp3Y1KEW-FeBL_yQs"
< Content-Type: application/json; charset=UTF-8
Content-Type: application/json; charset=UTF-8
< X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
X-XSS-Protection: 1; mode=block
< Server: GSE
Server: GSE
< Transfer-Encoding: chunked
Transfer-Encoding: chunked

< 
{
 "kind": "urlshortener#url",
 "id": "http://goo.gl/austZ",
 "longUrl": "https://developers.google.com/url-shortener/v1/getting_started?hl=ko"
}
* Connection #0 to host www.googleapis.com left intact
* Closing connection #0
* SSLv3, TLS alert, Client hello (1):
downloads$ 
**************** 아래는 원래의 url을 찾는 부분 입니다.
downloads$ curl -i -v -X GET 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/austZ'
* About to connect() to www.googleapis.com port 443 (#0)
*   Trying 74.125.31.95...
* connected
* Connected to www.googleapis.com (74.125.31.95) port 443 (#0)
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using RC4-SHA
* Server certificate:
* 	 subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.googleapis.com
* 	 start date: 2013-04-24 15:08:20 GMT
* 	 expire date: 2013-12-30 15:08:20 GMT
* 	 subjectAltName: www.googleapis.com matched
* 	 issuer: C=US; O=Google Inc; CN=Google Internet Authority
* 	 SSL certificate verify ok.
> GET /urlshortener/v1/url?shortUrl=http://goo.gl/austZ HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin10.8.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5 libidn/1.17
> Host: www.googleapis.com
> Accept: */*
> 
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Expires: Thu, 09 May 2013 09:42:12 GMT
Expires: Thu, 09 May 2013 09:42:12 GMT
< Date: Thu, 09 May 2013 09:37:12 GMT
Date: Thu, 09 May 2013 09:37:12 GMT
< Cache-Control: public, max-age=300, must-revalidate, no-transform
Cache-Control: public, max-age=300, must-revalidate, no-transform
< ETag: "1Q2LP9og6bfH7yqsv2dGqHDwZTM/JlHHbMe7mdp9nF1MEiBxErS-SZw"
ETag: "1Q2LP9og6bfH7yqsv2dGqHDwZTM/JlHHbMe7mdp9nF1MEiBxErS-SZw"
< Content-Type: application/json; charset=UTF-8
Content-Type: application/json; charset=UTF-8
< X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
X-XSS-Protection: 1; mode=block
< Server: GSE
Server: GSE
< Transfer-Encoding: chunked
Transfer-Encoding: chunked

< 
{
 "kind": "urlshortener#url",
 "id": "http://goo.gl/austZ",
 "longUrl": "https://developers.google.com/url-shortener/v1/getting_started?hl=ko",
 "status": "OK"
}
* Connection #0 to host www.googleapis.com left intact
* Closing connection #0
* SSLv3, TLS alert, Client hello (1):
downloads$