You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/guides/basics-of-authentication.md
+31-24Lines changed: 31 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,32 +7,33 @@ title: Basics of Authentication | GitHub API
7
7
* TOC
8
8
{:toc}
9
9
10
-
In this section, we're going to focus on the basics of authentication. Specifically,
11
-
we're going to create a Ruby server (using [Sinatra][Sinatra]) that implements
10
+
In this section, we're going to focus on the basics of authentication. Specifically,
11
+
we're going to create a Ruby server (using [Sinatra][Sinatra]) that implements
12
12
the [web flow][webflow] of an application in several different ways.
13
13
14
14
Note: you can download the complete source code for this project [from the platform-samples repo](https://github.com/github/platform-samples/tree/master/api/ruby/basics-of-authentication).
15
15
16
16
## Registering your app
17
17
18
18
First, you'll need to [register your
19
-
application](https://github.com/settings/applications/new). Every
20
-
registered OAuth application is assigned a unique Client ID and Client Secret.
19
+
application](https://github.com/settings/applications/new). Every
20
+
registered OAuth application is assigned a unique Client ID and Client Secret.
21
21
The Client Secret should not be shared! That includes checking the string
22
22
into your repository.
23
23
24
-
You can fill out every piece of information however you like, except the
25
-
**Authorization callback URL**. This is easily the most important piece to setting
26
-
up your application. It's the callback URL that GitHub returns the user to after
24
+
You can fill out every piece of information however you like, except the
25
+
**Authorization callback URL**. This is easily the most important piece to setting
26
+
up your application. It's the callback URL that GitHub returns the user to after
27
27
successful authentication.
28
28
29
-
Since we're running a regular Sinatra server, the location of the local instance
29
+
Since we're running a regular Sinatra server, the location of the local instance
30
30
is set to `http://localhost:4567`. Let's fill in the callback URL as `http://localhost:4567/callback`.
31
31
32
32
## Accepting user authorization
33
33
34
34
Now, let's start filling out our simple server. Create a file called _server.rb_ and paste this into it:
35
35
36
+
#!ruby
36
37
require 'sinatra'
37
38
require 'rest-client'
38
39
@@ -43,42 +44,43 @@ Now, let's start filling out our simple server. Create a file called _server.rb_
43
44
erb :index, :locals => {:client_id => CLIENT_ID}
44
45
end
45
46
46
-
Your client ID and client secret keys come from [your application's configuration page](https://github.com/settings/applications). You should **never, _ever_** store these values in
47
+
Your client ID and client secret keys come from [your application's configuration page](https://github.com/settings/applications). You should **never, _ever_** store these values in
47
48
GitHub--or any other public place, for that matter. We recommend storing them as
48
49
[environment variables][about env vars]--which is exactly what we've done here.
49
50
50
51
Next, in _views/index.erb_, paste this content:
51
52
52
-
53
+
#!html+erb
53
54
<html>
54
55
<head>
55
56
</head>
56
57
<body>
57
58
<p>Well, hello there!</p>
58
59
<p>We're going to now talk to the GitHub API. Ready? <a href="https://github.com/login/oauth/authorize?client_id=<%= client_id %>">Click here</a> to begin!</a></p>
59
-
<p>If that link doesn't work, remember to provide your own <a href="http://developer.github.com/v3/oauth/#web-application-flow">Client ID</a>!</p>
60
+
<p>If that link doesn't work, remember to provide your own <a href="/v3/oauth/#web-application-flow">Client ID</a>!</p>
60
61
</body>
61
62
</html>
62
63
63
64
(If you're unfamiliar with how Sinatra works, we recommend [reading the Sinatra guide][Sinatra guide].)
64
65
65
-
Obviously, you'll want to change `<your_client_id>` to match your actual Client ID.
66
+
Obviously, you'll want to change `<your_client_id>` to match your actual Client ID.
66
67
67
-
Navigate your browser to `http://localhost:4567`. After clicking on the link, you
68
+
Navigate your browser to `http://localhost:4567`. After clicking on the link, you
68
69
should be taken to GitHub, and presented with a dialog that looks something like this:
69
70

70
71
71
-
If you trust yourself, click **Authorize App**. Wuh-oh! Sinatra spits out a
72
+
If you trust yourself, click **Authorize App**. Wuh-oh! Sinatra spits out a
72
73
`404` error. What gives?!
73
74
74
75
Well, remember when we specified a Callback URL to be `callback`? We didn't provide
75
-
a route for it, so GitHub doesn't know where to drop the user after they authorize
76
+
a route for it, so GitHub doesn't know where to drop the user after they authorize
76
77
the app. Let's fix that now!
77
78
78
79
### Providing a callback
79
80
80
81
In _server.rb_, add a route to specify what the callback should do:
0 commit comments