**Prepare the Authorization header.** Per `section 3.5.1`_ of the spec. Protocol parameters can be transmitted using the HTTP "Authorization" header field as defined by `RFC2617`_ with the auth-scheme name set to "OAuth" (case insensitive). For example:: Authorization:
(oauth_params, headers=None, realm=None)
| 14 | |
| 15 | |
| 16 | def prepare_headers(oauth_params, headers=None, realm=None): |
| 17 | """**Prepare the Authorization header.** |
| 18 | Per `section 3.5.1`_ of the spec. |
| 19 | |
| 20 | Protocol parameters can be transmitted using the HTTP "Authorization" |
| 21 | header field as defined by `RFC2617`_ with the auth-scheme name set to |
| 22 | "OAuth" (case insensitive). |
| 23 | |
| 24 | For example:: |
| 25 | |
| 26 | Authorization: OAuth realm="Photos", |
| 27 | oauth_consumer_key="dpf43f3p2l4k3l03", |
| 28 | oauth_signature_method="HMAC-SHA1", |
| 29 | oauth_timestamp="137131200", |
| 30 | oauth_nonce="wIjqoS", |
| 31 | oauth_callback="http%3A%2F%2Fprinter.example.com%2Fready", |
| 32 | oauth_signature="74KNZJeDHnMBp0EMJ9ZHt%2FXKycU%3D", |
| 33 | oauth_version="1.0" |
| 34 | |
| 35 | .. _`section 3.5.1`: https://tools.ietf.org/html/rfc5849#section-3.5.1 |
| 36 | .. _`RFC2617`: https://tools.ietf.org/html/rfc2617 |
| 37 | """ |
| 38 | headers = headers or {} |
| 39 | |
| 40 | # step 1, 2, 3 in Section 3.5.1 |
| 41 | header_parameters = ", ".join( |
| 42 | [ |
| 43 | f'{escape(k)}="{escape(v)}"' |
| 44 | for k, v in oauth_params |
| 45 | if k.startswith("oauth_") |
| 46 | ] |
| 47 | ) |
| 48 | |
| 49 | # 4. The OPTIONAL "realm" parameter MAY be added and interpreted per |
| 50 | # `RFC2617 section 1.2`_. |
| 51 | # |
| 52 | # .. _`RFC2617 section 1.2`: https://tools.ietf.org/html/rfc2617#section-1.2 |
| 53 | if realm: |
| 54 | # NOTE: realm should *not* be escaped |
| 55 | header_parameters = f'realm="{realm}", ' + header_parameters |
| 56 | |
| 57 | # the auth-scheme name set to "OAuth" (case insensitive). |
| 58 | headers["Authorization"] = f"OAuth {header_parameters}" |
| 59 | return headers |
| 60 | |
| 61 | |
| 62 | def _append_params(oauth_params, params): |