fetch with authentication code example
Example 1: how to send basic auth using fetch
fetch(url, {
...options,
headers: {
'Authorization': 'Basic ' + btoa(`${username}:${password}`)
}
})
.then(response => response.json())
.then(json => console.log(json));
Example 2: node google client api to get user profile with already fetched token
var google = require('googleapis').google;
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2();
oauth2Client.setCredentials({access_token: 'ACCESS TOKEN HERE'});
var oauth2 = google.oauth2({
auth: oauth2Client,
version: 'v2'
});
oauth2.userinfo.get(
function(err, res) {
if (err) {
console.log(err);
} else {
console.log(res);
}
});