@@ -13,23 +13,25 @@ There's no easier way to kick the tires than through [cURL][curl].
1313
1414### Hello World
1515
16- Let's start by testing our setup:
16+ Let's start by testing our setup. Open up a command prompt and paste the following code :
1717
1818 curl https://api.github.com/zen
1919
20+ You should get a random response about one of our design philosophies:
21+
2022 Keep it logically awesome.
2123
2224Next, let's ` GET ` Chris Wanstrath's GitHub profile:
2325
2426 # GET /users/defunkt
2527 curl https://api.github.com/users/defunkt
2628
27- Mmmmm tastes like JSON. Let's include the ` -i ` flag to include headers:
29+ Mmmmm, tastes like JSON. Let's include the ` -i ` flag to include headers:
2830
2931 curl -i https://api.github.com/users/defunkt
3032
3133 HTTP/1.1 200 OK
32- Server: nginx
34+ Server: GitHub.com
3335 Date: Sun, 11 Nov 2012 18:43:28 GMT
3436 Content-Type: application/json; charset=utf-8
3537 Connection: keep-alive
@@ -44,34 +46,37 @@ Mmmmm tastes like JSON. Let's include the `-i` flag to include headers:
4446 Content-Length: 692
4547 Last-Modified: Tue, 30 Oct 2012 18:58:42 GMT
4648
47- There's a few interesting bits in the response headers. As we expected, the
48- ` Content-Type ` is ` application/json ` . Note the ` X-GitHub-Media-Type ` value of
49- ` github.beta ` . This lets us know the [ media type ] [ media types ] for the
50- response. Media types have helped us version our output in API v3. More on that
51- later.
49+ There's a few interesting bits in the response headers. As expected, the
50+ ` Content-Type ` is ` application/json ` .
51+
52+ Any headers beginning with ` X- ` are custom headers, and are not included in the
53+ HTTP spec. Let's take a look at a few of them:
5254
53- Take note of the ` X-RateLimit-Limit ` and ` X-RateLimit-Remaining ` headers. (Any
54- headers beginning with ` X- ` are custom headers and are not included in the HTTP
55- spec). This pair of headers indicate how many requests a client can make in a
56- rolling hour and how many of those requests it has already spent.
55+ * ` X-GitHub-Media-Type ` has a value of ` github.beta ` . This lets us know the [ media type] [ media types ]
56+ for the response. Media types have helped us version our output in API v3. We'll
57+ talk more about that later.
58+ * Take note of the ` X-RateLimit-Limit ` and ` X-RateLimit-Remaining ` headers. This
59+ pair of headers indicate how many requests a client can make in a rolling hour
60+ and how many of those requests it has already spent.
5761
5862## Authentication
5963
60- Unauthenticated clients can make 60 calls per hour. To get more, we need to
61- _ authenticate_ .
64+ Unauthenticated clients can make 60 calls per hour. To get more, we'll need to
65+ _ authenticate_ . In fact, doing anything interesting with the GitHub API requires
66+ authentication.
6267
6368### Basic
6469
65- The easiest way to authenticate with the GitHub API is using your GitHub
70+ The easiest way to authenticate with the GitHub API is by simply using your GitHub
6671username and password via Basic Authentication.
6772
68- curl -i -u tlberglund https://api.github.com/users/defunkt
69- Enter host password for user 'tlberglund ':
73+ curl -i -u <your_username> https://api.github.com/users/defunkt
74+ Enter host password for user '<your_username> ':
7075
71- The ` -u ` flag sets the username and cURL will prompt us for the password. You
76+ The ` -u ` flag sets the username, and cURL will prompt you for the password. You
7277can use ` -u "username:password" ` to avoid the prompt, but this leaves your
7378password in shell history and isn't recommended. When authenticating, you
74- should see your rate limit bumped to 5000 requests an hour as indicated in the
79+ should see your rate limit bumped to 5000 requests an hour, as indicated in the
7580` X-RateLimit-Limit ` header.
7681
7782In addition to just getting more calls per hour, authentication is the key to
@@ -81,9 +86,9 @@ reading and writing private information via the API.
8186
8287When properly authenticated, you can grab your own user profile:
8388
84- curl -i -u pengwynn https://api.github.com/user
89+ curl -i -u <your_username> https://api.github.com/user
8590
86- This time, in addition to the same set of information we retrieved for @ defunkt
91+ This time, in addition to the same set of information we retrieved for defunkt
8792earlier, you should see a ` plan ` object on the response:
8893
8994 ...
@@ -92,8 +97,7 @@ earlier, you should see a `plan` object on the response:
9297 "collaborators": 10,
9398 "private_repos": 20,
9499 "name": "medium"
95- },
96- "login": "pengwynn"
100+ }
97101 ...
98102
99103
@@ -106,19 +110,23 @@ private information using the API on behalf of another user should use [OAuth][o
106110Instead of usernames and passwords, OAuth uses _ tokens_ . Tokens provide two big
107111features:
108112
109- * ** Revokable access** . Users can revoke authorization to third party apps.
110- * ** Limited access** . Users can specify what access a token provides when they
111- authorize a third party app.
113+ * ** Revokable access** : users can revoke authorization to third party apps at any time
114+ * ** Limited access** : users can specify what access a token provides when they
115+ authorize a third party app
116+
117+ Normally, tokens are created via a [ web flow] [ webflow ] . An application will send
118+ users to GitHub to log in. GitHub will present a dialog indicating the name of the
119+ app, as well as what information it has access to. After a user authorizes access,
120+ GitHub redirects the user back to the application:
121+ ![ ] ( /images/oauth_prompt.png )
112122
113- Normally, tokens are created via a [ web flow] [ webflow ] , where third party
114- applications send users to GitHub to log in and authorize their application and
115- GitHub redirects the user back to the third party application. You don't need
116- to set up the entire web flow to begin working with OAuth tokens. The [ Authorizations API] [ authorizations api ]
117- makes it simple to use Basic Auth to create an OAuth token.
123+ You don't need to set up the entire web flow to begin working with OAuth tokens.
124+ The [ Authorizations API] [ authorizations api ] makes it simple to use Basic Auth
125+ to create an OAuth token. Try pasting and running
118126
119- curl -i -u pengwynn \
120- -d '{"scopes": ["repo"]}' \
121- https://api.github.com/authorizations
127+ curl -i -u <your_username> \
128+ -d '{"scopes": ["repo"]}' \
129+ https://api.github.com/authorizations
122130
123131 HTTP/1.1 201 Created
124132 Server: nginx/1.0.14
@@ -152,28 +160,29 @@ makes it simple to use Basic Auth to create an OAuth token.
152160
153161There's a lot going on in this one little call, so let's break it down. First,
154162the ` -d ` flag indicates we're doing a ` POST ` , using the
155- ` application/x-www-form-urlencoded ` content type. All ` POST ` requests to the
156- GitHub API should be JSON, as we're sending in this case .
163+ ` application/x-www-form-urlencoded ` content type (as opposed to ` GET ` ) . All ` POST `
164+ requests to the GitHub API should be in JSON .
157165
158166Next, let's look at the ` scopes ` we're sending over in this call. When creating
159167a new token, we include an optional array of [ _ scopes_ ] [ scopes ] , or access
160168levels, that indicate what information this token can access. In this case,
161169we're setting up the token with _ repo_ access, the most permissive scope in the
162170GitHub API, allowing access to read and write to private repositories. See [ the
163- docs] [ scopes ] for a full list of scopes.
171+ scopes docs] [ scopes ] for a full list of scopes. You should ** only** request
172+ scopes that your application actually needs, in order to not freighten users with
173+ potentially invasive actions.
164174
165- The ` 201 ` status tells us the call was successful and the JSON returned
166- contains the details of our new OAuth token. Now we can use the forty character
167- ` token ` instead of username and password in the rest of our examples. Let's
175+ The ` 201 ` status code tells us that the call was successful, and the JSON returned
176+ contains the details of our new OAuth token. Now, we can use the forty character
177+ ` token ` instead of a username and password in the rest of our examples. Let's
168178grab our own user info again, using OAuth this time:
169179
170180 curl -i -H 'Authorization: token 5199831f4dd3b79e7c5b7e0ebe75d67aa66e79d4' \
171181 https://api.github.com/user
172182
173- A quick note about tokens. ** Treat OAuth tokens like passwords.**
174- Don't share them with other users or store them in insecure places. The tokens
175- in these examples are fake and the names have been changed to protect the
176- innocent.
183+ ** Treat OAuth tokens like passwords!** Don't share them with other users or store
184+ them in insecure places. The tokens in these examples are fake and the names have
185+ been changed to protect the innocent.
177186
178187Now that we've got the hang of making authenticated calls, let's move along to
179188the [ Repositories API] [ repos-api ] .
@@ -191,12 +200,11 @@ In the same way, we can view repositories for the authenticated user:
191200 curl -i -H 'Authorization: token 5199831f4dd3b79e7c5b7e0ebe75d67aa66e79d4' \
192201 https://api.github.com/user/repos
193202
194- ... list repositories for another user:
195-
203+ Or, we can list repositories for another user:
196204
197205 curl -i https://api.github.com/users/technoweenie/repos
198206
199- ... or list repositories for an organization:
207+ Or, we can list repositories for an organization:
200208
201209 curl -i https://api.github.com/orgs/github/repos
202210
@@ -207,21 +215,20 @@ The information returned from these calls will depend on how we authenticate:
207215
208216As the [ docs] [ repos-api ] indicate, these methods take a ` type ` parameter that
209217can filter the repositories returned based on what type of access the user has
210- for the repository. In this way we can fetch only directly-owned repositories,
211- organization repositories, or repositories the user collaborates on via a
212- team.
218+ for the repository. In this way, we can fetch only directly-owned repositories,
219+ organization repositories, or repositories the user collaborates on via a team.
213220
214221 curl -i "https://api.github.com/users/technoweenie/repos?type=owner"
215222
216- In this example, we can grab only those repositories that Rick owns, not the
223+ In this example, we can grab only those repositories that technoweenie owns, not the
217224ones on which he collaborates. Note the quoted URL above. Depending on your
218225shell setup, cURL sometimes requires a quoted URL or else it ignores the
219226querystring.
220227
221228### Create a repository
222229
223230Fetching information for existing repositories is a common use case, but the
224- GitHub API supports creating new repositories as well. To create a repository
231+ GitHub API supports creating new repositories as well. To create a repository,
225232we need to ` POST ` some JSON containing the details and configuration options.
226233
227234 curl -i -H 'Authorization: token 5199831f4dd3b79e7c5b7e0ebe75d67aa66e79d4' \
@@ -234,17 +241,17 @@ we need to `POST` some JSON containing the details and configuration options.
234241 https://api.github.com/user/repos
235242
236243In this minimal example, we create a new repository for our blog (to be served
237- on [ GitHub Pages] [ pages ] perhaps). Though the blog will be public, we've made
238- the repository private. In this single step, we'll also init it with
244+ on [ GitHub Pages] [ pages ] , perhaps). Though the blog will be public, we've made
245+ the repository private. In this single step, we'll also initialize it with
239246a README and a [ nanoc] [ nanoc ] -flavored [ .gitignore template] [ gitignore
240247templates] .
241248
242249The resulting repository will be found at `https://github.com/ <your
243- usrname >/blog`. To create a repository under an organization for which you're
250+ username >/blog`. To create a repository under an organization for which you're
244251an owner, just change the API method from ` /user/repos ` to `/orgs/{org
245252name}/repos`.
246253
247- Let 's fetch our newly created repository:
254+ Next, let 's fetch our newly created repository:
248255
249256 curl -i https://api.github.com/pengwynn/blog
250257
@@ -257,7 +264,7 @@ Let's fetch our newly created repository:
257264Oh noes! Where did it go? Since we created the repository as _ private_ , we need
258265to authenticate in order to see it. If you're a grizzled HTTP user, you might
259266expect a ` 403 ` instead. Since we don't want to leak information about private
260- repositories, the GitHub API returns a ` 404 ` instead as if to say "we can
267+ repositories, the GitHub API returns a ` 404 ` instead, as if to say "we can
261268neither confirm nor deny the existence of this repository."
262269
263270## Issues
@@ -268,16 +275,14 @@ data out or create issues from other tools to create a workflow that works for
268275your team.
269276
270277Just like github.com, the API provides a few methods to view issues for the
271- authenticated user. To see all your issues, ` GET /issues ` :
272-
278+ authenticated user. To see all your issues, call ` GET /issues ` :
273279
274280 curl -i -H 'Authorization: token 5199831f4dd3b79e7c5b7e0ebe75d67aa66e79d4' \
275281 https://api.github.com/issues
276282
277- To get only the issues under one of your GitHub organizations, `GET
283+ To get only the issues under one of your GitHub organizations, call `GET
278284/orgs/{org}/issues`:
279285
280-
281286 curl -i -H 'Authorization: token 5199831f4dd3b79e7c5b7e0ebe75d67aa66e79d4' \
282287 https://api.github.com/orgs/rails/issues
283288
@@ -316,7 +321,7 @@ the API.
316321 }' \
317322 https://api.github.com/repos/pengwynn/api-sandbox/issues
318323
319- To create an issue, we need to be authenticated so we pass an
324+ To create an issue, we need to be authenticated, so we pass an
320325OAuth token in the header. We pass the title, body, and labels in the JSON
321326body to the ` /issues ` path underneath the repository in which we want to create
322327the issue.
@@ -387,7 +392,7 @@ from the issue we provide.
387392A big part of being a good API citizen is respecting rate limits and caching
388393information that does not change. The API supports [ conditional
389394requests] [ conditional-requests ] and helps you do the right thing. Consider the
390- first call we made to get @ defunkt 's profile:
395+ first call we made to get defunkt's profile:
391396
392397 curl -i https://api.github.com/users/defunkt
393398
0 commit comments