Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(291)

Side by Side Diff: chrome/common/extensions/docs/tut_oauth.html

Issue 10642015: Basic setup for generating app docs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 <!DOCTYPE html><!-- This page is a placeholder for generated extensions api doc. Note:
2 1) The <head> information in this page is significant, should be uniform
3 across api docs and should be edited only with knowledge of the
4 templating mechanism.
5 3) All <body>.innerHTML is genereated as an rendering step. If viewed in a
6 browser, it will be re-generated from the template, json schema and
7 authored overview content.
8 4) The <body>.innerHTML is also generated by an offline step so that this
9 page may easily be indexed by search engines.
10 --><html xmlns="http://www.w3.org/1999/xhtml"><head>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12 <link href="css/ApiRefStyles.css" rel="stylesheet" type="text/css">
13 <link href="css/print.css" rel="stylesheet" type="text/css" media="print">
14 <script type="text/javascript" src="../../../third_party/jstemplate/jstempla te_compiled.js">
15 </script>
16 <script type="text/javascript" src="../../../../third_party/json_minify/mini fy-sans-regexp.js">
17 </script>
18 <script type="text/javascript" src="js/api_page_generator.js"></script>
19 <script type="text/javascript" src="js/bootstrap.js"></script>
20 <script type="text/javascript" src="js/sidebar.js"></script>
21 <title>Tutorial: OAuth - Google Chrome Extensions - Google Code</title></head>
22 <body> <div id="devModeWarning" class="displayModeWarning">
23 You are viewing extension docs in chrome via the 'file:' scheme: are you exp ecting to see local changes when you refresh? You'll need run chrome with --allo w-file-access-from-files.
24 </div>
25 <div id="branchWarning" class="displayModeWarning">
26 <span>WARNING: This is the <span id="branchName">BETA</span> documentation.
27 It may not work with the stable release of Chrome.</span>
28 <select id="branchChooser">
29 <option>Choose a different version...
30 </option><option value="">Stable
31 </option><option value="beta">Beta
32 </option><option value="dev">Dev
33 </option><option value="trunk">Trunk
34 </option></select>
35 </div>
36 <div id="unofficialWarning" class="displayModeWarning">
37 <span>WARNING: This is unofficial documentation. It may not work with the
38 current release of Chrome.</span>
39 <button id="goToOfficialDocs">Go to the official docs</button>
40 </div>
41 <div id="gc-container" class="labs">
42 <!-- SUBTEMPLATES: DO NOT MOVE FROM THIS LOCATION -->
43 <!-- In particular, sub-templates that recurse, must be used by allowing
44 jstemplate to make a copy of the template in this section which
45 are not operated on by way of the jsskip="true" -->
46 <!-- /SUBTEMPLATES -->
47 <a id="top"></a>
48 <div id="skipto">
49 <a href="#gc-pagecontent">Skip to page content</a>
50 <a href="#gc-toc">Skip to main navigation</a>
51 </div>
52 <!-- API HEADER -->
53 <table id="header" width="100%" cellspacing="0" border="0">
54 <tbody><tr>
55 <td valign="middle"><a href="http://code.google.com/"><img src="images/c ode_labs_logo.gif" height="43" width="161" alt="Google Code Labs" style="border: 0; margin:0;"></a></td>
56 <td valign="middle" width="100%" style="padding-left:0.6em;">
57 <form action="http://www.google.com/cse" id="cse" style="margin-top:0. 5em">
58 <div id="gsc-search-box">
59 <input type="hidden" name="cx" value="002967670403910741006:61_cvz fqtno">
60 <input type="hidden" name="ie" value="UTF-8">
61 <input type="text" name="q" value="" size="55">
62 <input class="gsc-search-button" type="submit" name="sa" value="Se arch">
63 <br>
64 <span class="greytext">e.g. "page action" or "tabs"</span>
65 </div>
66 </form>
67 <script type="text/javascript" src="https://www.google.com/jsapi"></sc ript>
68 <script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script>
69 <script type="text/javascript" src="https://www.google.com/coop/cse/t1 3n?form=cse&amp;t13n_langs=en"></script>
70 <script type="text/javascript" src="https://www.google.com/coop/cse/br and?form=cse&amp;lang=en"></script>
71 </td>
72 </tr>
73 </tbody></table>
74 <div id="codesiteContent" class="">
75 <a id="gc-topnav-anchor"></a>
76 <div id="gc-topnav">
77 <h1>Google Chrome Extensions (<a href="http://code.google.com/labs/">Lab s</a>)</h1>
78 <ul id="home" class="gc-topnav-tabs">
79 <li id="home_link">
80 <a href="index.html" title="Google Chrome Extensions home page">Home </a>
81 </li>
82 <li id="docs_link">
83 <a href="docs.html" title="Official Google Chrome Extensions documen tation">Docs</a>
84 </li>
85 <li id="faq_link">
86 <a href="faq.html" title="Answers to frequently asked questions abou t Google Chrome Extensions">FAQ</a>
87 </li>
88 <li id="samples_link">
89 <a href="samples.html" title="Sample extensions (with source code)"> Samples</a>
90 </li>
91 <li id="group_link">
92 <a href="http://groups.google.com/a/chromium.org/group/chromium-exte nsions" title="Google Chrome Extensions developer forum">Group</a>
93 </li>
94 <li id="so_link">
95 <a href="http://stackoverflow.com/questions/tagged/google-chrome-ext ension" title="[google-chrome-extension] tag on Stack Overflow">Questions?</a>
96 </li>
97 </ul>
98 </div> <!-- end gc-topnav -->
99 <div class="g-section g-tpl-170">
100 <!-- SIDENAV -->
101 <div class="g-unit g-first" id="gc-toc">
102 <ul>
103 <li><a href="getstarted.html">Getting Started</a></li>
104 <li><a href="overview.html">Overview</a></li>
105 <li><a href="whats_new.html">What's New?</a></li>
106 <li><h2><a href="devguide.html">Developer's Guide</a></h2>
107 <ul>
108 <li>Browser UI
109 <ul>
110 <li><a href="browserAction.html">Browser Actions</a></li>
111 <li><a href="contextMenus.html">Context Menus</a></li>
112 <li><a href="notifications.html">Desktop Notifications</a></li >
113 <li><a href="omnibox.html">Omnibox</a></li>
114 <li><a href="options.html">Options Pages</a></li>
115 <li><a href="override.html">Override Pages</a></li>
116 <li><a href="pageAction.html">Page Actions</a></li>
117 </ul>
118 </li>
119 <li>Browser Interaction
120 <ul>
121 <li><a href="bookmarks.html">Bookmarks</a></li>
122 <li><a href="cookies.html">Cookies</a></li>
123 <li><a href="devtools.html">Developer Tools</a></li>
124 <li><a href="events.html">Events</a></li>
125 <li><a href="history.html">History</a></li>
126 <li><a href="management.html">Management</a></li>
127 <li><a href="tabs.html">Tabs</a></li>
128 <li><a href="windows.html">Windows</a></li>
129 </ul>
130 </li>
131 <li>Implementation
132 <ul>
133 <li><a href="a11y.html">Accessibility</a></li>
134 <li><a href="background_pages.html">Background Pages</a></li>
135 <li><a href="content_scripts.html">Content Scripts</a></li>
136 <li><a href="xhr.html">Cross-Origin XHR</a></li>
137 <li><a href="i18n.html">Internationalization</a></li>
138 <li><a href="messaging.html">Message Passing</a></li>
139 <li><a href="permissions.html">Optional Permissions</a></li>
140 <li><a href="npapi.html">NPAPI Plugins</a></li>
141 </ul>
142 </li>
143 <li>Finishing
144 <ul>
145 <li><a href="hosting.html">Hosting</a></li>
146 <li><a href="external_extensions.html">Other Deployment Option s</a></li>
147 </ul>
148 </li>
149 </ul>
150 </li>
151 <li><h2><a href="apps.html">Packaged Apps</a></h2></li>
152 <li><h2><a href="tutorials.html">Tutorials</a></h2>
153 <ul>
154 <li><a href="tut_debugging.html">Debugging</a></li>
155 <li><a href="tut_analytics.html">Google Analytics</a></li>
156 <li class="leftNavSelected">OAuth</li>
157 </ul>
158 </li>
159 <li><h2>Reference</h2>
160 <ul>
161 <li>Formats
162 <ul>
163 <li><a href="manifest.html">Manifest Files</a></li>
164 <li><a href="match_patterns.html">Match Patterns</a></li>
165 </ul>
166 </li>
167 <li><a href="permission_warnings.html">Permission Warnings</a></li >
168 <li><a href="api_index.html">chrome.* APIs</a></li>
169 <li><a href="api_other.html">Other APIs</a></li>
170 </ul>
171 </li>
172 <li><h2><a href="samples.html">Samples</a></h2></li>
173 <div class="line"> </div>
174 <li><h2>More</h2>
175 <ul>
176 <li><a href="http://code.google.com/chrome/webstore/docs/index.htm l">Chrome Web Store</a></li>
177 <li><a href="http://code.google.com/chrome/apps/docs/developers_gu ide.html">Hosted Apps</a></li>
178 <li><a href="themes.html">Themes</a></li>
179 </ul>
180 </li>
181 </ul>
182 </div>
183 <script>
184 initToggles();
185 </script>
186 <div class="g-unit" id="gc-pagecontent">
187 <div id="pageTitle">
188 <h1 class="page_title">Tutorial: OAuth</h1>
189 </div>
190 <!-- TABLE OF CONTENTS -->
191 <div id="toc">
192 <h2>Contents</h2>
193 <ol>
194 <li>
195 <a href="#requirements">Requirements</a>
196 <ol>
197 </ol>
198 </li><li>
199 <a href="#getting-started">Getting started</a>
200 <ol>
201 </ol>
202 </li><li>
203 <a href="#oauth-dance">The OAuth dance in an extension</a>
204 <ol>
205 <li>
206 <a href="#set-code">Setup code</a>
207 </li><li>
208 <a href="#request-token">Fetching and authorizing a request to ken</a>
209 </li><li>
210 <a href="#signed-requests">Sending signed API requests</a>
211 </li>
212 </ol>
213 </li><li>
214 <a href="#sample-code">Sample code</a>
215 <ol>
216 </ol>
217 </li>
218 </ol>
219 </div>
220 <!-- /TABLE OF CONTENTS -->
221 <!-- Standard content lead-in for experimental API pages -->
222 <!-- STATIC CONTENT PLACEHOLDER -->
223 <div id="static"><div id="pageData-name" class="pageData">Tutorial: OAut h</div>
224 <div id="pageData-showTOC" class="pageData">true</div>
225 <p>
226 <a href="http://oauth.net/">OAuth</a> is an open protocol that aims to standardi ze the way desktop and web applications access a user's private data. OAuth prov ides a mechanism for users to grant access to private data without sharing their private credentials (username/password). Many sites have started enabling APIs to use OAuth because of its security and standard set of libraries.
227 </p>
228 <p>
229 This tutorial will walk you through the necessary steps for creating a Google Ch rome Extension that uses OAuth to access an API. It leverages a library that you can reuse in your extensions.
230 </p>
231 <p>
232 This tutorial uses the <a href="http://code.google.com/apis/documents/">Google D ocuments List Data API</a> as an example OAuth-enabled API endpoint.
233 </p>
234 <h2 id="requirements">Requirements</h2>
235 <p>
236 This tutorial expects that you have some experience writing extensions for Googl e Chrome and some familiarity with the <a href="http://code.google.com/apis/acco unts/docs/OAuth.html">3-legged OAuth</a> flow. Although you don’t need a backgro und in the <a href="http://code.google.com/apis/documents/">Google Documents Lis t Data API</a> (or the other <a href="http://code.google.com/apis/gdata/">Google Data APIs</a> for that matter), having a understanding of the protocol may be h elpful.
237 </p>
238 <h2 id="getting-started">Getting started</h2>
239 <p>
240 First, copy over the three library files from the Chromium source tree at <a hre f="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs /examples/extensions/oauth_contacts/">.../examples/extensions/oauth_contacts/</a >:
241 </p>
242 <ul>
243 <li><strong><a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/comm on/extensions/docs/examples/extensions/oauth_contacts/chrome_ex_oauth.html?revis ion=34725&amp;content-type=text/plain">chrome_ex_oauth.html</a></strong> - inter stitial page for the oauth_callback URL</li>
244 <li><strong><a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/comm on/extensions/docs/examples/extensions/oauth_contacts/chrome_ex_oauth.js?content -type=text/plain">chrome_ex_oauth.js</a></strong> - core OAuth library</li>
245 <li><strong><a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/comm on/extensions/docs/examples/extensions/oauth_contacts/chrome_ex_oauthsimple.js?c ontent-type=text/plain">chrome_ex_oauthsimple.js</a></strong> - helpful wrapper for chrome_ex_oauth.js</li>
246 </ul>
247 <p>Place the three library files in the root of your extension directory (or whe rever your JavaScript is stored). Then include both .js files in your background page in the following order:</p>
248 <pre>&lt;script type="text/javascript" src="chrome_ex_oauthsimple.js"&gt;&lt;/sc ript&gt;
249 &lt;script type="text/javascript" src="chrome_ex_oauth.js"&gt;&lt;/script&gt;
250 </pre>
251 <p>Your background page will manage the OAuth flow.</p>
252 <h2 id="oauth-dance">The OAuth dance in an extension</h2>
253 <p>
254 If you are familiar with the OAuth protocol, you'll recall that the OAuth dance consists of three steps:
255 </p>
256 <ol>
257 <li>fetching an initial request token</li>
258 <li>having the user authorize the request token</li>
259 <li>fetching an access token</li>
260 </ol>
261 <p>In the context of an extension, this flow gets a bit tricky. Namely, there is no established consumer key/secret between the service provider and the applica tion. That is, there is no web application URL for the user to be redirected to after the approval process.
262 </p>
263 <p>
264 Luckily, Google and a few other companies have been working on an <a href="http: //code.google.com/apis/accounts/docs/OAuthForInstalledApps.html">OAuth for insta lled applications</a> solution that you can use from an extension environment. I n the installed applications OAuth dance, the consumer key/secret are ‘anonymous ’/’anonymous’ and you provide an <em>application name</em> for the user to grant access to (instead of an application URL). The end result is the same: your bac kground page requests the initial token, opens a new tab to the approval page, a nd finally makes the asynchronous call for the access token.
265 </p>
266 <h3 id="set-code">Setup code</h3>
267 <p>To initialize the library, create a <code>ChromeExOAuth</code> object in the background page:</p>
268 <pre>var oauth = ChromeExOAuth.initBackgroundPage({
269 'request_url': &lt;OAuth request URL&gt;,
270 'authorize_url': &lt;OAuth authorize URL&gt;,
271 'access_url': &lt;OAuth access token URL&gt;,
272 'consumer_key': &lt;OAuth consumer key&gt;,
273 'consumer_secret': &lt;OAuth consumer secret&gt;,
274 'scope': &lt;scope of data access, not used by all OAuth providers&gt;,
275 'app_name': &lt;application name, not used by all OAuth providers&gt;
276 });
277 </pre>
278 <p>In the case of the Documents List API and Google’s OAuth endpoints, a possibl e initialization may be:</p>
279 <pre>var oauth = ChromeExOAuth.initBackgroundPage({
280 'request_url': 'https://www.google.com/accounts/OAuthGetRequestToken',
281 'authorize_url': 'https://www.google.com/accounts/OAuthAuthorizeToken',
282 'access_url': 'https://www.google.com/accounts/OAuthGetAccessToken',
283 'consumer_key': 'anonymous',
284 'consumer_secret': 'anonymous',
285 'scope': 'https://docs.google.com/feeds/',
286 'app_name': 'My Google Docs Extension'
287 });
288 </pre>
289 <h3 id="request-token">Fetching and authorizing a request token</h3>
290 <p>
291 Once you have your background page set up, call the <code>authorize()</code> fun ction to begin the OAuth dance and redirect the user to the OAuth provider. The client library abstracts most of this process, so all you need to do is pass a c allback to the <code>authorize()</code> function, and a new tab will open and re direct the user.
292 </p>
293 <pre>oauth.authorize(function() {
294 // ... Ready to fetch private data ...
295 });
296 </pre>
297 <p>
298 You don't need to provide any additional logic for storing the token and secret, as this library already stores these values in the browser’s <code>localStorage </code>. If the library already has an access token stored for the current scope , then no tab will be opened. In either case, the callback will be called.
299 </p>
300 <h3 id="signed-requests">Sending signed API requests</h3>
301 <p>
302 Once your specified callback is executed, call the <code>sendSignedRequest()</co de> function to send signed requests to your API endpoint(s). <code>sendSignedRe quest()</code> takes three arguments: a URI, a callback function, and an optiona l parameter object. The callback is passed two arguments: the response text and the <code>XMLHttpRequest</code> object that was used to make the request.
303 </p>
304 <p>This example sends an HTTP <code>GET</code>:</p>
305 <pre>function callback(resp, xhr) {
306 // ... Process text response ...
307 };
308 function onAuthorized() {
309 var url = 'https://docs.google.com/feeds/default/private/full';
310 var request = {
311 'method': 'GET',
312 'parameters': {'alt': 'json'}
313 };
314 // Send: GET https://docs.google.com/feeds/default/private/full?alt=json
315 oauth.sendSignedRequest(url, callback, request);
316 };
317 oauth.authorize(onAuthorized);
318 </pre>
319 <p>A more complex example using an HTTP <code>POST</code> might look like this:< /p>
320 <pre>function onAuthorized() {
321 var url = 'https://docs.google.com/feeds/default/private/full';
322 var request = {
323 'method': 'POST',
324 'headers': {
325 'GData-Version': '3.0',
326 'Content-Type': 'application/atom+xml'
327 },
328 'parameters': {
329 'alt': 'json'
330 },
331 'body': 'Data to send'
332 };
333 // Send: POST https://docs.google.com/feeds/default/private/full?alt=json
334 oauth.sendSignedRequest(url, callback, request);
335 };
336 </pre>
337 <p>
338 By default, the <code>sendSignedRequest()</code> function sends the <code>oauth_ *</code> parameters in the URL (by calling <code>oauth.signURL()</code>). If you prefer to send the <code>oauth_*</code> parameters in the <code>Authorization</ code> header (or need direct access to the generated header), use <code>getAutho rizationHeader()</code>. Its arguments are a URI, an HTTP method, and an optiona l object of URL query parameters as key/value pairs.
339 </p>
340 <p>Here is the example above using <code>getAuthorizationHeader()</code> and an <code>XMLHttpRequest</code> object:</p>
341 <pre>function stringify(parameters) {
342 var params = [];
343 for(var p in parameters) {
344 params.push(encodeURIComponent(p) + '=' +
345 encodeURIComponent(parameters[p]));
346 }
347 return params.join('&amp;');
348 };
349 function onAuthorized() {
350 var method = 'POST';
351 var url = 'https://docs.google.com/feeds/default/private/full';
352 var params = {'alt': 'json'};
353 var xhr = new XMLHttpRequest();
354 xhr.onreadystatechange = function(data) {
355 callback(xhr, data);
356 };
357 xhr.setRequestHeader('GData-Version', '3.0');
358 xhr.setRequestHeader('Content-Type', 'application/atom+xml');
359 xhr.setRequestHeader('Authorization', oauth.getAuthorizationHeader(url, method , params));
360 xhr.open(method, url + '?' + stringify(params), true);
361 xhr.send('Data to send');
362 };
363 </pre>
364 <h2 id="sample-code">Sample code</h2>
365 <p>
366 Sample extensions that use these techniques are available in the Chromium source tree:
367 </p>
368 <ul>
369 <li><a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/exten sions/docs/examples/extensions/gdocs/">.../examples/extensions/gdocs/</a></li>
370 <li><a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/exten sions/docs/examples/extensions/oauth_contacts/">.../examples/extensions/oauth_co ntacts/</a></li>
371 </ul>
372 </div>
373 <!-- API PAGE -->
374 <!-- /apiPage -->
375 </div> <!-- /gc-pagecontent -->
376 </div> <!-- /g-section -->
377 </div> <!-- /codesiteContent -->
378 <div id="gc-footer" --="">
379 <div class="text">
380 <p>
381 Except as otherwise <a href="http://code.google.com/policies.html#restrictions ">noted</a>,
382 the content of this page is licensed under the <a rel="license" href="http://c reativecommons.org/licenses/by/3.0/">Creative Commons
383 Attribution 3.0 License</a>, and code samples are licensed under the
384 <a rel="license" href="http://code.google.com/google_bsd_license.html">BSD Lic ense</a>.
385 </p>
386 <p>
387 ©2011 Google
388 </p>
389 <!-- begin analytics -->
390 <script src="https://www.google-analytics.com/urchin.js" type="text/javascript"> </script>
391 <script src="https://www.google-analytics.com/ga.js" type="text/javascript"></sc ript>
392 <script type="text/javascript">
393 // chrome doc tracking
394 try {
395 var engdocs = _gat._getTracker("YT-10763712-2");
396 engdocs._trackPageview();
397 } catch(err) {}
398 // code.google.com site-wide tracking
399 try {
400 _uacct="UA-18071-1";
401 _uanchor=1;
402 _uff=0;
403 urchinTracker();
404 }
405 catch(e) {/* urchinTracker not available. */}
406 </script>
407 <!-- end analytics -->
408 </div>
409 </div> <!-- /gc-footer -->
410 </div> <!-- /gc-container -->
411 </body></html>
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/tut_debugging.html ('k') | chrome/common/extensions/docs/tutorials.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698