oauth1_session
OAuth1Session
Bases: Session
Request signing and convenience methods for the oauth dance.
What is the difference between OAuth1Session and OAuth1?
OAuth1Session actually uses OAuth1 internally and its purpose is to assist in the OAuth workflow through convenience methods to prepare authorization URLs and parse the various token and redirection responses. It also provide rudimentary validation of responses.
An example of the OAuth workflow using a basic CLI app and Twitter.
Credentials obtained during the registration.
client_key = 'client key' client_secret = 'secret' callback_uri = 'https://127.0.0.1/callback'
Endpoints found in the OAuth provider API documentation
request_token_url = 'https://api.twitter.com/oauth/request_token' authorization_url = 'https://api.twitter.com/oauth/authorize' access_token_url = 'https://api.twitter.com/oauth/access_token'
oauth_session = OAuth1Session(client_key,client_secret=client_secret, callback_uri=callback_uri)
First step, fetch the request token.
oauth_session.fetch_request_token(request_token_url) { 'oauth_token': 'kjerht2309u', 'oauth_token_secret': 'lsdajfh923874', }
Second step. Follow this link and authorize
oauth_session.authorization_url(authorization_url) 'https://api.twitter.com/oauth/authorize?oauth_token=sdf0o9823sjdfsdf&oauth_callback=https%3A%2F%2F127.0.0.1%2Fcallback'
Third step. Fetch the access token
redirect_response = input('Paste the full redirect URL here.') oauth_session.parse_authorization_response(redirect_response) { 'oauth_token: 'kjerht2309u', 'oauth_token_secret: 'lsdajfh923874', 'oauth_verifier: 'w34o8967345', } oauth_session.fetch_access_token(access_token_url) { 'oauth_token': 'sdf0o9823sjdfsdf', 'oauth_token_secret': '2kjshdfp92i34asdasd', }
Done. You can now make OAuth requests.
status_url = 'http://api.twitter.com/1/statuses/update.json' new_status = {'status': 'hello world!'} oauth_session.post(status_url, data=new_status)
Source code in server/vendor/requests_oauthlib/oauth1_session.py
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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
|
authorized
property
Boolean that indicates whether this session has an OAuth token or not. If self.authorized
is True, you can reasonably expect OAuth-protected requests to the resource to succeed. If self.authorized
is False, you need the user to go through the OAuth authentication dance before OAuth-protected requests to the resource will succeed.
__init__(client_key, client_secret=None, resource_owner_key=None, resource_owner_secret=None, callback_uri=None, signature_method=SIGNATURE_HMAC, signature_type=SIGNATURE_TYPE_AUTH_HEADER, rsa_key=None, verifier=None, client_class=None, force_include_body=False, **kwargs)
Construct the OAuth 1 session.
:param client_key: A client specific identifier. :param client_secret: A client specific secret used to create HMAC and plaintext signatures. :param resource_owner_key: A resource owner key, also referred to as request token or access token depending on when in the workflow it is used. :param resource_owner_secret: A resource owner secret obtained with either a request or access token. Often referred to as token secret. :param callback_uri: The URL the user is redirect back to after authorization. :param signature_method: Signature methods determine how the OAuth signature is created. The three options are oauthlib.oauth1.SIGNATURE_HMAC (default), oauthlib.oauth1.SIGNATURE_RSA and oauthlib.oauth1.SIGNATURE_PLAIN. :param signature_type: Signature type decides where the OAuth parameters are added. Either in the Authorization header (default) or to the URL query parameters or the request body. Defined as oauthlib.oauth1.SIGNATURE_TYPE_AUTH_HEADER, oauthlib.oauth1.SIGNATURE_TYPE_QUERY and oauthlib.oauth1.SIGNATURE_TYPE_BODY respectively. :param rsa_key: The private RSA key as a string. Can only be used with signature_method=oauthlib.oauth1.SIGNATURE_RSA. :param verifier: A verifier string to prove authorization was granted. :param client_class: A subclass of oauthlib.oauth1.Client
to use with requests_oauthlib.OAuth1
instead of the default :param force_include_body: Always include the request body in the signature creation. :param **kwargs: Additional keyword arguments passed to OAuth1
Source code in server/vendor/requests_oauthlib/oauth1_session.py
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 |
|
authorization_url(url, request_token=None, **kwargs)
Create an authorization URL by appending request_token and optional kwargs to url.
This is the second step in the OAuth 1 workflow. The user should be redirected to this authorization URL, grant access to you, and then be redirected back to you. The redirection back can either be specified during client registration or by supplying a callback URI per request.
:param url: The authorization endpoint URL. :param request_token: The previously obtained request token. :param kwargs: Optional parameters to append to the URL. :returns: The authorization URL with new parameters embedded.
An example using a registered default callback URI.
request_token_url = 'https://api.twitter.com/oauth/request_token' authorization_url = 'https://api.twitter.com/oauth/authorize' oauth_session = OAuth1Session('client-key', client_secret='secret') oauth_session.fetch_request_token(request_token_url) { 'oauth_token': 'sdf0o9823sjdfsdf', 'oauth_token_secret': '2kjshdfp92i34asdasd', } oauth_session.authorization_url(authorization_url) 'https://api.twitter.com/oauth/authorize?oauth_token=sdf0o9823sjdfsdf' oauth_session.authorization_url(authorization_url, foo='bar') 'https://api.twitter.com/oauth/authorize?oauth_token=sdf0o9823sjdfsdf&foo=bar'
An example using an explicit callback URI.
request_token_url = 'https://api.twitter.com/oauth/request_token' authorization_url = 'https://api.twitter.com/oauth/authorize' oauth_session = OAuth1Session('client-key', client_secret='secret', callback_uri='https://127.0.0.1/callback') oauth_session.fetch_request_token(request_token_url) { 'oauth_token': 'sdf0o9823sjdfsdf', 'oauth_token_secret': '2kjshdfp92i34asdasd', } oauth_session.authorization_url(authorization_url) 'https://api.twitter.com/oauth/authorize?oauth_token=sdf0o9823sjdfsdf&oauth_callback=https%3A%2F%2F127.0.0.1%2Fcallback'
Source code in server/vendor/requests_oauthlib/oauth1_session.py
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 |
|
fetch_access_token(url, verifier=None, **request_kwargs)
Fetch an access token.
This is the final step in the OAuth 1 workflow. An access token is obtained using all previously obtained credentials, including the verifier from the authorization step.
Note that a previously set verifier will be reset for your convenience, or else signature creation will be incorrect on consecutive requests.
access_token_url = 'https://api.twitter.com/oauth/access_token' redirect_response = 'https://127.0.0.1/callback?oauth_token=kjerht2309uf&oauth_token_secret=lsdajfh923874&oauth_verifier=w34o8967345' oauth_session = OAuth1Session('client-key', client_secret='secret') oauth_session.parse_authorization_response(redirect_response) { 'oauth_token: 'kjerht2309u', 'oauth_token_secret: 'lsdajfh923874', 'oauth_verifier: 'w34o8967345', } oauth_session.fetch_access_token(access_token_url) { 'oauth_token': 'sdf0o9823sjdfsdf', 'oauth_token_secret': '2kjshdfp92i34asdasd', }
Source code in server/vendor/requests_oauthlib/oauth1_session.py
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 |
|
fetch_request_token(url, realm=None, **request_kwargs)
Fetch a request token.
This is the first step in the OAuth 1 workflow. A request token is obtained by making a signed post request to url. The token is then parsed from the application/x-www-form-urlencoded response and ready to be used to construct an authorization url.
:param url: The request token endpoint URL. :param realm: A list of realms to request access to. :param request_kwargs: Optional arguments passed to ''post'' function in ''requests.Session'' :returns: The response in dict format.
Note that a previously set callback_uri will be reset for your convenience, or else signature creation will be incorrect on consecutive requests.
request_token_url = 'https://api.twitter.com/oauth/request_token' oauth_session = OAuth1Session('client-key', client_secret='secret') oauth_session.fetch_request_token(request_token_url) { 'oauth_token': 'sdf0o9823sjdfsdf', 'oauth_token_secret': '2kjshdfp92i34asdasd', }
Source code in server/vendor/requests_oauthlib/oauth1_session.py
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 |
|
parse_authorization_response(url)
Extract parameters from the post authorization redirect response URL.
:param url: The full URL that resulted from the user being redirected back from the OAuth provider to you, the client. :returns: A dict of parameters extracted from the URL.
redirect_response = 'https://127.0.0.1/callback?oauth_token=kjerht2309uf&oauth_token_secret=lsdajfh923874&oauth_verifier=w34o8967345' oauth_session = OAuth1Session('client-key', client_secret='secret') oauth_session.parse_authorization_response(redirect_response) { 'oauth_token: 'kjerht2309u', 'oauth_token_secret: 'lsdajfh923874', 'oauth_verifier: 'w34o8967345', }
Source code in server/vendor/requests_oauthlib/oauth1_session.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
|
rebuild_auth(prepared_request, response)
When being redirected we should always strip Authorization header, since nonce may not be reused as per OAuth spec.
Source code in server/vendor/requests_oauthlib/oauth1_session.py
385 386 387 388 389 390 391 392 393 394 395 |
|
TokenRequestDenied
Bases: ValueError
Source code in server/vendor/requests_oauthlib/oauth1_session.py
26 27 28 29 30 31 32 33 34 |
|
status_code
property
For backwards-compatibility purposes
urldecode(body)
Parse query or json to python dictionary
Source code in server/vendor/requests_oauthlib/oauth1_session.py
16 17 18 19 20 21 22 23 |
|