Merge master · barkinet/developer.github.com@dea96de · GitHub
Skip to content

Commit dea96de

Browse files
committed
Merge master
2 parents 490c71e + 99b8e2a commit dea96de

64 files changed

Lines changed: 352 additions & 290 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Rules

Lines changed: 1 addition & 0 deletions

content/changes/2013-10-04-oauth-changes-coming.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
For example, if you are making a POST with the `application/json`
1111
mime-type you'll see an additional field for the granted scopes.
1212

13-
<pre class="highlight"><code class="language-javascript">
13+
<pre><code class="language-javascript">
1414
{
1515
"access_token":"e72e16c7e42f292c6912e7710c838347ae178b4a",
1616
"scope":"repo,gist",

content/guides/basics-of-authentication.md

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,33 @@ title: Basics of Authentication | GitHub API
77
* TOC
88
{:toc}
99

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
1212
the [web flow][webflow] of an application in several different ways.
1313

1414
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).
1515

1616
## Registering your app
1717

1818
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.
2121
The Client Secret should not be shared! That includes checking the string
2222
into your repository.
2323

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
2727
successful authentication.
2828

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
3030
is set to `http://localhost:4567`. Let's fill in the callback URL as `http://localhost:4567/callback`.
3131

3232
## Accepting user authorization
3333

3434
Now, let's start filling out our simple server. Create a file called _server.rb_ and paste this into it:
3535

36+
#!ruby
3637
require 'sinatra'
3738
require 'rest-client'
3839

@@ -43,42 +44,43 @@ Now, let's start filling out our simple server. Create a file called _server.rb_
4344
erb :index, :locals => {:client_id => CLIENT_ID}
4445
end
4546

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
4748
GitHub--or any other public place, for that matter. We recommend storing them as
4849
[environment variables][about env vars]--which is exactly what we've done here.
4950

5051
Next, in _views/index.erb_, paste this content:
5152

52-
53+
#!html+erb
5354
<html>
5455
<head>
5556
</head>
5657
<body>
5758
<p>Well, hello there!</p>
5859
<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>
6061
</body>
6162
</html>
6263

6364
(If you're unfamiliar with how Sinatra works, we recommend [reading the Sinatra guide][Sinatra guide].)
6465

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.
6667

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
6869
should be taken to GitHub, and presented with a dialog that looks something like this:
6970
![](/images/oauth_prompt.png)
7071

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
7273
`404` error. What gives?!
7374

7475
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
7677
the app. Let's fix that now!
7778

7879
### Providing a callback
7980

8081
In _server.rb_, add a route to specify what the callback should do:
8182

83+
#!ruby
8284
get '/callback' do
8385
# get temporary GitHub code...
8486
session_code = request.env['rack.request.query_hash']["code"]
@@ -94,20 +96,22 @@ In _server.rb_, add a route to specify what the callback should do:
9496
end
9597

9698
After a successful app authentication, GitHub provides a temporary `code` value.
97-
You'll need to `POST` this code back to GitHub in exchange for an `access_token`.
99+
You'll need to `POST` this code back to GitHub in exchange for an `access_token`.
98100
To simplify our GET and POST HTTP requests, we're using the [rest-client][REST Client].
99-
Note that you'll probably never access the API through REST. For a more serious
101+
Note that you'll probably never access the API through REST. For a more serious
100102
application, you should probably use [a library written in the language of your choice][libraries].
101103

102104
At last, with this access token, you'll be able to make authenticated requests as
103105
the logged in user:
104106

107+
#!ruby
105108
auth_result = RestClient.get("https://api.github.com/user", {:params => {:access_token => access_token}})
106109

107110
erb :basic, :locals => {:auth_result => auth_result}
108111

109112
We can do whatever we want with our results. In this case, we'll just dump them straight into _basic.erb_:
110113

114+
#!html+erb
111115
<p>Okay, here's a JSON dump:</p>
112116
<p>
113117
<p>Hello, <%= login %>! It looks like you're <%= hire_status %>.</p>
@@ -116,12 +120,12 @@ We can do whatever we want with our results. In this case, we'll just dump them
116120
## Implementing "persistent" authentication
117121

118122
It'd be a pretty bad model if we required users to log into the app every single
119-
time they needed to access the web page. For example, try navigating directly to
123+
time they needed to access the web page. For example, try navigating directly to
120124
`http://localhost:4567/basic`. You'll get an error.
121125

122126
What if we could circumvent the entire
123127
"click here" process, and just _remember_ that, as long as the user's logged into
124-
GitHub, they should be able to access this application? Hold on to your hat,
128+
GitHub, they should be able to access this application? Hold on to your hat,
125129
because _that's exactly what we're going to do_.
126130

127131
Our little server above is rather simple. In order to wedge in some intelligent
@@ -133,6 +137,7 @@ This will make authentication transparent to the user.
133137
After you run `gem install sinatra_auth_github`, create a file called _advanced_server.rb_,
134138
and paste these lines into it:
135139

140+
#!ruby
136141
require 'sinatra/auth/github'
137142
require 'rest-client'
138143

@@ -164,7 +169,7 @@ and paste these lines into it:
164169
authenticate!
165170
else
166171
access_token = github_user["token"]
167-
auth_result = RestClient.get("https://api.github.com/user", {:params => {:access_token => access_token, :accept => :json},
172+
auth_result = RestClient.get("https://api.github.com/user", {:params => {:access_token => access_token, :accept => :json},
168173
:accept => :json})
169174

170175
auth_result = JSON.parse(auth_result)
@@ -184,14 +189,14 @@ and paste these lines into it:
184189
end
185190
end
186191

187-
Much of the code should look familiar. For example, we're still using `RestClient.get`
192+
Much of the code should look familiar. For example, we're still using `RestClient.get`
188193
to call out to the GitHub API, and we're still passing our results to be rendered
189194
in an ERB template (this time, it's called `advanced.erb`). Some of the other
190195
details--like turning our app into a class that inherits from `Sinatra::Base`--are a result
191196
of inheriting from `sinatra/auth/github`, which is written as [a Sinatra extension][sinatra extension].
192197

193198
Also, we now have a `github_user` object, which comes from `sinatra-auth-github`. The
194-
`token` key represents the same `access_token` we used during our simple server.
199+
`token` key represents the same `access_token` we used during our simple server.
195200

196201
`sinatra-auth-github` comes with quite a few options that you can customize. Here,
197202
we're establishing them through the `:github_options` symbol. Passing your client ID
@@ -201,6 +206,7 @@ to simplify your authentication.
201206
We must also create a _config.ru_ config file, which Rack will use for its configuration
202207
options:
203208

209+
#!ruby
204210
ENV['RACK_ENV'] ||= 'development'
205211
require "rubygems"
206212
require "bundler/setup"
@@ -211,6 +217,7 @@ options:
211217

212218
Next, create a file in _views_ called _advanced.erb_, and paste this markup into it:
213219

220+
#!html+erb
214221
<html>
215222
<head>
216223
</head>
@@ -235,12 +242,12 @@ we would've seen the same confirmation dialog from earlier pop-up and warn us.
235242
If you'd like, you can play around with [yet another Sinatra-GitHub auth example][sinatra auth github test]
236243
available as a separate project.
237244

238-
[webflow]: http://developer.github.com/v3/oauth/#web-application-flow
245+
[webflow]: /v3/oauth/#web-application-flow
239246
[Sinatra]: http://www.sinatrarb.com/
240247
[about env vars]: http://en.wikipedia.org/wiki/Environment_variable#Getting_and_setting_environment_variables
241248
[Sinatra guide]: http://sinatra-book.gittr.com/#hello_world_application
242249
[REST Client]: https://github.com/archiloque/rest-client
243-
[libraries]: http://developer.github.com/v3/libraries/
250+
[libraries]: /libraries/
244251
[rack guide]: http://en.wikipedia.org/wiki/Rack_(web_server_interface)
245252
[sinatra auth github]: https://github.com/atmos/sinatra_auth_github
246253
[sinatra extension]: http://www.sinatrarb.com/extensions.html

content/guides/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ responses don't count against your [rate limit][rate-limiting].
444444

445445

446446

447-
[wrappers]: http://developer.github.com/v3/libraries/
447+
[wrappers]: http://developer.github.com/libraries/
448448
[curl]: http://curl.haxx.se/
449449
[media types]: http://developer.github.com/v3/media/
450450
[oauth]: http://developer.github.com/v3/oauth/

content/guides/rendering-data-as-graphs.md

Lines changed: 47 additions & 34 deletions

0 commit comments

Comments
 (0)