API – Stepan Suvorov Blog https://stepansuvorov.com/blog Release 2.0 Wed, 26 Jul 2017 12:20:03 +0000 en-US hourly 1 https://wordpress.org/?v=6.3.1 Mocky – nice HTTP mocking service https://stepansuvorov.com/blog/2017/07/mocky-nice-http-mocking-service/ https://stepansuvorov.com/blog/2017/07/mocky-nice-http-mocking-service/#respond Wed, 26 Jul 2017 12:20:03 +0000 http://stepansuvorov.com/blog/?p=3422 I found easy to use mocking service – Mocky – that allows you to check not only with fixed response for GET/POST…, but also to emit any HTTP error and setup custom header.

]]>
https://stepansuvorov.com/blog/2017/07/mocky-nice-http-mocking-service/feed/ 0
Minimal set for Github OAuth with AngularJS and Node.js https://stepansuvorov.com/blog/2015/03/minimal-set-for-github-oauth-with-angularjs-and-node-js/ https://stepansuvorov.com/blog/2015/03/minimal-set-for-github-oauth-with-angularjs-and-node-js/#respond Thu, 12 Mar 2015 19:41:26 +0000 http://stepansuvorov.com/blog/?p=2437 Continue reading ]]> I so many times had returned back to this topic that decided to create kind of hint/note for myself and probably it also could be useful for the audience.

Applications on Github

To make possible github OAuth on your app you first should to register your application there. And get set of necessary params:

  • Client ID
  • Client Secret

Algorithm

I’ll not go in all the details, because there are a lot of manuals in internet. Only resume. In 7 steps:

  1. web application provides user authorisation link to github
  2. user follows this link and confirms authorisation
  3. github redirects user back to our site with special code parameter in url
  4. our web application gets this code parameter and sends it to the server
  5. server creates OAuth request based on this code and our api secret and sends it to github
  6. github responses to our server with OAuth token
  7. server provide token to the web application

Authorisation link

it’s just a link of such format:

https://github.com/login/oauth/authorize?client_id=%YOUR_APP_ID%

Web Application part

I’ve created simple example of Angular service that will send code parameter to the server:

[javascript]
angular.module(‘config-builder’)
.service(‘Github’, function($http) {

var token = null;

this.getTokenPromise = function(code) {
return $http.get(‘/api/github/token/’ + code).then(function(result){
token = result.data.access_token;
});
};
});
[/javascript]

Server side

And server side part to send OAuth token request:

[javascript]
app.get(‘/api/github/token/:code’, function(req, res) {
request.post({
uri: ‘https://github.com/login/oauth/access_token’,
form: {
client_id: ‘%YOUR_APP_ID%’,
client_secret: ‘%YOUR_APP_SECRET%’,
code: req.params.code
},
json: true
}, function(err, httpResponse, body) {
if (err) {
res.send(500, { error: err });
return;
}
res.send(body);
});
});
[/javascript]

app – is an instance of express framework in this case.

That’s all!

]]>
https://stepansuvorov.com/blog/2015/03/minimal-set-for-github-oauth-with-angularjs-and-node-js/feed/ 0