Skip to content

Instantly share code, notes, and snippets.

@thiphariel
Created October 26, 2014 18:34
Show Gist options
  • Select an option

  • Save thiphariel/ac288bcff19f1b792ed1 to your computer and use it in GitHub Desktop.

Select an option

Save thiphariel/ac288bcff19f1b792ed1 to your computer and use it in GitHub Desktop.
ghost api call authentication token
var postData = querystring.stringify({
grant_type: "password",
username: "<username>",
password: "<password>",
client_id: "ghost-admin"
});
var uriBase = '/ghost/api/v0.1/';
var requestOptions = {
host: 'hostname',
port: 80,
path: uriBase + 'authentication/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
};
var request = http.request(requestOptions, function(response) {
response.setEncoding('utf8');
var authData = '';
response.on('data', function(chunk) {
authData += chunk;
})
response.on('end', function() {
var auth = JSON.parse(authData);
console.log(auth);
requestOptions.headers.Authorization = auth.token_type + ' ' + auth.access_token;
var apiOptions = {
url: 'http://' + requestOptions.host + ':' + requestOptions.port + uriBase + '/users/me',
headers: requestOptions.headers
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info);
}
}
requester(apiOptions, callback);
});
});
request.write(postData);
request.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment