/usr/share/doc/python-requests-oauthlib-doc/html/oauth2_workflow.html is in python-requests-oauthlib-doc 0.7.0-0.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OAuth 2 Workflow — Requests-OAuthlib 0.7.0 documentation</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.7.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="top" title="Requests-OAuthlib 0.7.0 documentation" href="index.html" />
<link rel="next" title="Examples" href="examples/examples.html" />
<link rel="prev" title="OAuth 1 Workflow" href="oauth1_workflow.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="examples/examples.html" title="Examples"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="oauth1_workflow.html" title="OAuth 1 Workflow"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Requests-OAuthlib 0.7.0 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="oauth-2-workflow">
<h1>OAuth 2 Workflow<a class="headerlink" href="#oauth-2-workflow" title="Permalink to this headline">¶</a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#introduction" id="id5">Introduction</a><ul>
<li><a class="reference internal" href="#available-workflows" id="id6">Available Workflows</a></li>
</ul>
</li>
<li><a class="reference internal" href="#web-application-flow" id="id7">Web Application Flow</a></li>
<li><a class="reference internal" href="#mobile-application-flow" id="id8">Mobile Application Flow</a></li>
<li><a class="reference internal" href="#legacy-application-flow" id="id9">Legacy Application Flow</a></li>
<li><a class="reference internal" href="#backend-application-flow" id="id10">Backend Application Flow</a></li>
<li><a class="reference internal" href="#refreshing-tokens" id="id11">Refreshing tokens</a><ul>
<li><a class="reference internal" href="#all-define-the-token-token-saver-and-needed-credentials" id="id12">(ALL) Define the token, token saver and needed credentials</a></li>
<li><a class="reference internal" href="#first-define-try-catch-tokenexpirederror-on-each-request" id="id13">(First) Define Try-Catch TokenExpiredError on each request</a></li>
<li><a class="reference internal" href="#second-define-automatic-token-refresh-automatic-but-update-manually" id="id14">(Second) Define automatic token refresh automatic but update manually</a></li>
<li><a class="reference internal" href="#third-recommended-define-automatic-token-refresh-and-update" id="id15">(Third, Recommended) Define automatic token refresh and update</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="introduction">
<h2><a class="toc-backref" href="#id5">Introduction</a><a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>The following sections provide some example code that demonstrates some of the
possible OAuth2 flows you can use with requests-oauthlib. We provide four
examples: one for each of the grant types defined by the OAuth2 RFC. These
grant types (or workflows) are the Authorization Code Grant (or Web Application
Flow), the Implicit Grant (or Mobile Application Flow), the Resource Owner
Password Credentials Grant (or, more succinctly, the Legacy Application Flow),
and the Client Credentials Grant (or Backend Application Flow).</p>
<div class="section" id="available-workflows">
<h3><a class="toc-backref" href="#id6">Available Workflows</a><a class="headerlink" href="#available-workflows" title="Permalink to this headline">¶</a></h3>
<p>There are four core work flows:</p>
<ol class="arabic simple">
<li><a class="reference internal" href="#web-application-flow"><span class="std std-ref">Authorization Code Grant</span></a> (Web Application
Flow).</li>
<li><a class="reference internal" href="#mobile-application-flow"><span class="std std-ref">Implicit Grant</span></a> (Mobile Application flow).</li>
<li><a class="reference internal" href="#legacy-application-flow"><span class="std std-ref">Resource Owner Password Credentials Grant</span></a>
(Legacy Application flow).</li>
<li><a class="reference internal" href="#backend-application-flow"><span class="std std-ref">Client Credentials Grant</span></a> (Backend
Application flow).</li>
</ol>
</div>
</div>
<div class="section" id="web-application-flow">
<span id="id1"></span><h2><a class="toc-backref" href="#id7">Web Application Flow</a><a class="headerlink" href="#web-application-flow" title="Permalink to this headline">¶</a></h2>
<p>The steps below outline how to use the default Authorization Grant Type flow to
obtain an access token and fetch a protected resource. In this example
the provider is Google and the protected resource is the user’s profile.</p>
<ol class="arabic simple" start="0">
<li>Obtain credentials from your OAuth provider manually. At minimum you will
need a <code class="docutils literal"><span class="pre">client_id</span></code> but likely also a <code class="docutils literal"><span class="pre">client_secret</span></code>. During this
process you might also be required to register a default redirect URI to be
used by your application. Save these things in your Python script:</li>
</ol>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">client_id</span> <span class="o">=</span> <span class="s1">r'your_client_id'</span>
<span class="gp">>>> </span><span class="n">client_secret</span> <span class="o">=</span> <span class="s1">r'your_client_secret'</span>
<span class="gp">>>> </span><span class="n">redirect_uri</span> <span class="o">=</span> <span class="s1">'https://your.callback/uri'</span>
</pre></div>
</div>
<ol class="arabic simple">
<li>User authorization through redirection. First we will create an
authorization url from the base URL given by the provider and
the credentials previously obtained. In addition most providers will
request that you ask for access to a certain scope. In this example
we will ask Google for access to the email address of the user and the
users profile.</li>
</ol>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="go"># Note that these are Google specific scopes</span>
<span class="gp">>>> </span><span class="n">scope</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'https://www.googleapis.com/auth/userinfo.email'</span><span class="p">,</span>
<span class="go"> 'https://www.googleapis.com/auth/userinfo.profile']</span>
<span class="gp">>>> </span><span class="n">oauth</span> <span class="o">=</span> <span class="n">OAuth2Session</span><span class="p">(</span><span class="n">client_id</span><span class="p">,</span> <span class="n">redirect_uri</span><span class="o">=</span><span class="n">redirect_uri</span><span class="p">,</span>
<span class="go"> scope=scope)</span>
<span class="gp">>>> </span><span class="n">authorization_url</span><span class="p">,</span> <span class="n">state</span> <span class="o">=</span> <span class="n">oauth</span><span class="o">.</span><span class="n">authorization_url</span><span class="p">(</span>
<span class="go"> 'https://accounts.google.com/o/oauth2/auth',</span>
<span class="go"> # access_type and approval_prompt are Google specific extra</span>
<span class="go"> # parameters.</span>
<span class="go"> access_type="offline", approval_prompt="force")</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="s1">'Please go to </span><span class="si">%s</span><span class="s1"> and authorize access.'</span> <span class="o">%</span> <span class="n">authorization_url</span>
<span class="gp">>>> </span><span class="n">authorization_response</span> <span class="o">=</span> <span class="nb">raw_input</span><span class="p">(</span><span class="s1">'Enter the full callback URL'</span><span class="p">)</span>
</pre></div>
</div>
<ol class="arabic simple" start="2">
<li>Fetch an access token from the provider using the authorization code
obtained during user authorization.</li>
</ol>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">token</span> <span class="o">=</span> <span class="n">oauth</span><span class="o">.</span><span class="n">fetch_token</span><span class="p">(</span>
<span class="go"> 'https://accounts.google.com/o/oauth2/token',</span>
<span class="go"> authorization_response=authorization_response,</span>
<span class="go"> # Google specific extra parameter used for client</span>
<span class="go"> # authentication</span>
<span class="go"> client_secret=client_secret)</span>
</pre></div>
</div>
<ol class="arabic simple" start="3">
<li>Access protected resources using the access token you just obtained.
For example, get the users profile info.</li>
</ol>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">r</span> <span class="o">=</span> <span class="n">oauth</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'https://www.googleapis.com/oauth2/v1/userinfo'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="c1"># Enjoy =)</span>
</pre></div>
</div>
</div>
<div class="section" id="mobile-application-flow">
<span id="id2"></span><h2><a class="toc-backref" href="#id8">Mobile Application Flow</a><a class="headerlink" href="#mobile-application-flow" title="Permalink to this headline">¶</a></h2>
<p>Documentation coming soon. Want to help? Why not <a class="reference external" href="https://github.com/requests/requests-oauthlib/issues/48">write this section</a>?</p>
</div>
<div class="section" id="legacy-application-flow">
<span id="id3"></span><h2><a class="toc-backref" href="#id9">Legacy Application Flow</a><a class="headerlink" href="#legacy-application-flow" title="Permalink to this headline">¶</a></h2>
<p>The steps below outline how to use the Resource Owner Password Credentials Grant Type flow to obtain an access token.</p>
<ol class="arabic simple" start="0">
<li>You will need the following settings. <code class="docutils literal"><span class="pre">client_secret</span></code> is optional depending on the provider.</li>
</ol>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">client_id</span> <span class="o">=</span> <span class="s1">'your_client_id'</span>
<span class="gp">>>> </span><span class="n">client_secret</span> <span class="o">=</span> <span class="s1">'your_client_secret'</span>
<span class="gp">>>> </span><span class="n">username</span> <span class="o">=</span> <span class="s1">'your_username'</span>
<span class="gp">>>> </span><span class="n">password</span> <span class="o">=</span> <span class="s1">'your_password'</span>
</pre></div>
</div>
<ol class="arabic simple">
<li>Fetch an access token from the provider.</li>
</ol>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">oauthlib.oauth2</span> <span class="kn">import</span> <span class="n">LegacyApplicationClient</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">requests_oauthlib</span> <span class="kn">import</span> <span class="n">OAuth2Session</span>
<span class="gp">>>> </span><span class="n">oauth</span> <span class="o">=</span> <span class="n">OAuth2Session</span><span class="p">(</span><span class="n">client</span><span class="o">=</span><span class="n">LegacyApplicationClient</span><span class="p">(</span><span class="n">client_id</span><span class="o">=</span><span class="n">client_id</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">token</span> <span class="o">=</span> <span class="n">oauth</span><span class="o">.</span><span class="n">fetch_token</span><span class="p">(</span><span class="n">token_url</span><span class="o">=</span><span class="s1">'https://somesite.com/oauth2/token'</span><span class="p">,</span>
<span class="go"> username=username, password=password, client_id=client_id,</span>
<span class="go"> client_secret=client_secret)</span>
</pre></div>
</div>
</div>
<div class="section" id="backend-application-flow">
<span id="id4"></span><h2><a class="toc-backref" href="#id10">Backend Application Flow</a><a class="headerlink" href="#backend-application-flow" title="Permalink to this headline">¶</a></h2>
<p>The steps below outline how to use the Resource Owner Client Credentials Grant Type flow to obtain an access token.</p>
<ol class="arabic simple" start="0">
<li>Obtain credentials from your OAuth provider. At minimum you will
need a <code class="docutils literal"><span class="pre">client_id</span></code> and <code class="docutils literal"><span class="pre">client_secret</span></code>.</li>
</ol>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">client_id</span> <span class="o">=</span> <span class="s1">'your_client_id'</span>
<span class="gp">>>> </span><span class="n">client_secret</span> <span class="o">=</span> <span class="s1">'your_client_secret'</span>
</pre></div>
</div>
<ol class="arabic simple">
<li>Fetch an access token from the provider.</li>
</ol>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">oauthlib.oauth2</span> <span class="kn">import</span> <span class="n">BackendApplicationClient</span>
<span class="gp">>>> </span><span class="n">client</span> <span class="o">=</span> <span class="n">BackendApplicationClient</span><span class="p">(</span><span class="n">client_id</span><span class="o">=</span><span class="n">client_id</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">oauth</span> <span class="o">=</span> <span class="n">OAuth2Session</span><span class="p">(</span><span class="n">client</span><span class="o">=</span><span class="n">client</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">token</span> <span class="o">=</span> <span class="n">oauth</span><span class="o">.</span><span class="n">fetch_token</span><span class="p">(</span><span class="n">token_url</span><span class="o">=</span><span class="s1">'https://provider.com/oauth2/token'</span><span class="p">,</span> <span class="n">client_id</span><span class="o">=</span><span class="n">client_id</span><span class="p">,</span>
<span class="go"> client_secret=client_secret)</span>
</pre></div>
</div>
</div>
<div class="section" id="refreshing-tokens">
<h2><a class="toc-backref" href="#id11">Refreshing tokens</a><a class="headerlink" href="#refreshing-tokens" title="Permalink to this headline">¶</a></h2>
<p>Certain providers will give you a <code class="docutils literal"><span class="pre">refresh_token</span></code> along with the
<code class="docutils literal"><span class="pre">access_token</span></code>. These can be used to directly fetch new access tokens without
going through the normal OAuth workflow. <code class="docutils literal"><span class="pre">requests-oauthlib</span></code> provides three
methods of obtaining refresh tokens. All of these are dependant on you
specifying an accurate <code class="docutils literal"><span class="pre">expires_in</span></code> in the token.</p>
<p><code class="docutils literal"><span class="pre">expires_in</span></code> is a credential given with the access and refresh token
indiciating in how many seconds from now the access token expires. Commonly,
access tokens expire after an hour an the <code class="docutils literal"><span class="pre">expires_in</span></code> would be <code class="docutils literal"><span class="pre">3600</span></code>.
Without this it is impossible for <code class="docutils literal"><span class="pre">requests-oauthlib</span></code> to know when a token
is expired as the status code of a request failing due to token expiration is
not defined.</p>
<p>If you are not interested in token refreshing, always pass in a positive value
for <code class="docutils literal"><span class="pre">expires_in</span></code> or omit it entirely.</p>
<div class="section" id="all-define-the-token-token-saver-and-needed-credentials">
<h3><a class="toc-backref" href="#id12">(ALL) Define the token, token saver and needed credentials</a><a class="headerlink" href="#all-define-the-token-token-saver-and-needed-credentials" title="Permalink to this headline">¶</a></h3>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">token</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span> <span class="s1">'access_token'</span><span class="p">:</span> <span class="s1">'eswfld123kjhn1v5423'</span><span class="p">,</span>
<span class="gp">... </span> <span class="s1">'refresh_token'</span><span class="p">:</span> <span class="s1">'asdfkljh23490sdf'</span><span class="p">,</span>
<span class="gp">... </span> <span class="s1">'token_type'</span><span class="p">:</span> <span class="s1">'Bearer'</span><span class="p">,</span>
<span class="gp">... </span> <span class="s1">'expires_in'</span><span class="p">:</span> <span class="s1">'-30'</span><span class="p">,</span> <span class="c1"># initially 3600, need to be updated by you</span>
<span class="gp">... </span> <span class="p">}</span>
<span class="gp">>>> </span><span class="n">client_id</span> <span class="o">=</span> <span class="s1">r'foo'</span>
<span class="gp">>>> </span><span class="n">refresh_url</span> <span class="o">=</span> <span class="s1">'https://provider.com/token'</span>
<span class="gp">>>> </span><span class="n">protected_url</span> <span class="o">=</span> <span class="s1">'https://provider.com/secret'</span>
<span class="gp">>>> </span><span class="c1"># most providers will ask you for extra credentials to be passed along</span>
<span class="gp">>>> </span><span class="c1"># when refreshing tokens, usually for authentication purposes.</span>
<span class="gp">>>> </span><span class="n">extra</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span> <span class="s1">'client_id'</span><span class="p">:</span> <span class="n">client_id</span><span class="p">,</span>
<span class="gp">... </span> <span class="s1">'client_secret'</span><span class="p">:</span> <span class="s1">r'potato'</span><span class="p">,</span>
<span class="gp">... </span><span class="p">}</span>
<span class="gp">>>> </span><span class="c1"># After updating the token you will most likely want to save it.</span>
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">token_saver</span><span class="p">(</span><span class="n">token</span><span class="p">):</span>
<span class="gp">... </span> <span class="c1"># save token in database / session</span>
</pre></div>
</div>
</div>
<div class="section" id="first-define-try-catch-tokenexpirederror-on-each-request">
<h3><a class="toc-backref" href="#id13">(First) Define Try-Catch TokenExpiredError on each request</a><a class="headerlink" href="#first-define-try-catch-tokenexpirederror-on-each-request" title="Permalink to this headline">¶</a></h3>
<p>This is the most basic version in which an error is raised when refresh
is necessary but refreshing is done manually.</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">requests_oauthlib</span> <span class="kn">import</span> <span class="n">OAuth2Session</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">oauthlib.oauth2</span> <span class="kn">import</span> <span class="n">TokenExpiredError</span>
<span class="gp">>>> </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">client</span> <span class="o">=</span> <span class="n">OAuth2Session</span><span class="p">(</span><span class="n">client_id</span><span class="p">,</span> <span class="n">token</span><span class="o">=</span><span class="n">token</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">r</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">protected_url</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">except</span> <span class="n">TokenExpiredError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">token</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">refresh_token</span><span class="p">(</span><span class="n">refresh_url</span><span class="p">,</span> <span class="o">**</span><span class="n">extra</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">token_saver</span><span class="p">(</span><span class="n">token</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">client</span> <span class="o">=</span> <span class="n">OAuth2Session</span><span class="p">(</span><span class="n">client_id</span><span class="p">,</span> <span class="n">token</span><span class="o">=</span><span class="n">token</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">r</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">protected_url</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="second-define-automatic-token-refresh-automatic-but-update-manually">
<h3><a class="toc-backref" href="#id14">(Second) Define automatic token refresh automatic but update manually</a><a class="headerlink" href="#second-define-automatic-token-refresh-automatic-but-update-manually" title="Permalink to this headline">¶</a></h3>
<p>This is the, arguably awkward, middle between the basic and convenient refresh
methods in which a token is automatically refreshed, but saving the new token
is done manually.</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">requests_oauthlib</span> <span class="kn">import</span> <span class="n">OAuth2Session</span><span class="p">,</span> <span class="n">TokenUpdated</span>
<span class="gp">>>> </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">client</span> <span class="o">=</span> <span class="n">OAuth2Session</span><span class="p">(</span><span class="n">client_id</span><span class="p">,</span> <span class="n">token</span><span class="o">=</span><span class="n">token</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">auto_refresh_kwargs</span><span class="o">=</span><span class="n">extra</span><span class="p">,</span> <span class="n">auto_refresh_url</span><span class="o">=</span><span class="n">refresh_url</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">r</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">protected_url</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">except</span> <span class="n">TokenUpdated</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">token_saver</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">token</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="third-recommended-define-automatic-token-refresh-and-update">
<h3><a class="toc-backref" href="#id15">(Third, Recommended) Define automatic token refresh and update</a><a class="headerlink" href="#third-recommended-define-automatic-token-refresh-and-update" title="Permalink to this headline">¶</a></h3>
<p>The third and recommended method will automatically fetch refresh tokens and
save them. It requires no exception catching and results in clean code. Remember
however that you still need to update <code class="docutils literal"><span class="pre">expires_in</span></code> to trigger the refresh.</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">requests_oauthlib</span> <span class="kn">import</span> <span class="n">OAuth2Session</span>
<span class="gp">>>> </span><span class="n">client</span> <span class="o">=</span> <span class="n">OAuth2Session</span><span class="p">(</span><span class="n">client_id</span><span class="p">,</span> <span class="n">token</span><span class="o">=</span><span class="n">token</span><span class="p">,</span> <span class="n">auto_refresh_url</span><span class="o">=</span><span class="n">refresh_url</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">auto_refresh_kwargs</span><span class="o">=</span><span class="n">extra</span><span class="p">,</span> <span class="n">token_updater</span><span class="o">=</span><span class="n">token_saver</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">r</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">protected_url</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">OAuth 2 Workflow</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a><ul>
<li><a class="reference internal" href="#available-workflows">Available Workflows</a></li>
</ul>
</li>
<li><a class="reference internal" href="#web-application-flow">Web Application Flow</a></li>
<li><a class="reference internal" href="#mobile-application-flow">Mobile Application Flow</a></li>
<li><a class="reference internal" href="#legacy-application-flow">Legacy Application Flow</a></li>
<li><a class="reference internal" href="#backend-application-flow">Backend Application Flow</a></li>
<li><a class="reference internal" href="#refreshing-tokens">Refreshing tokens</a><ul>
<li><a class="reference internal" href="#all-define-the-token-token-saver-and-needed-credentials">(ALL) Define the token, token saver and needed credentials</a></li>
<li><a class="reference internal" href="#first-define-try-catch-tokenexpirederror-on-each-request">(First) Define Try-Catch TokenExpiredError on each request</a></li>
<li><a class="reference internal" href="#second-define-automatic-token-refresh-automatic-but-update-manually">(Second) Define automatic token refresh automatic but update manually</a></li>
<li><a class="reference internal" href="#third-recommended-define-automatic-token-refresh-and-update">(Third, Recommended) Define automatic token refresh and update</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="oauth1_workflow.html"
title="previous chapter">OAuth 1 Workflow</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="examples/examples.html"
title="next chapter">Examples</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/oauth2_workflow.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="examples/examples.html" title="Examples"
>next</a> |</li>
<li class="right" >
<a href="oauth1_workflow.html" title="OAuth 1 Workflow"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Requests-OAuthlib 0.7.0 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2016, Kenneth Reitz.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.9.
</div>
</body>
</html>
|