Last active
September 18, 2023 14:39
-
-
Save magician11/37ee3c5a47be7655d3ef1601f6263194 to your computer and use it in GitHub Desktop.
How to use Request with the Google URL Shortener API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request'); | |
const GOOGLE_API_KEY = 'your api key'; // from https://console.developers.google.com/apis/credentials | |
const urlToShorten = 'http://www.thelongurltoshorten.com'; | |
const shortenerUrl = `https://www.googleapis.com/urlshortener/v1/url?key=${GOOGLE_API_KEY}`; | |
const options = { | |
uri: shortenerUrl, | |
json: { | |
longUrl: urlToShorten | |
} | |
}; | |
request.post(options, (error, response, body) => { | |
if (body.error) { | |
console.log(body.error); | |
} else if (response.statusCode === 200) { | |
console.log(body); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment