0

I would like to connect to the bricklink API but i am struggling with how to pass the auth parameter's into my get request. I've tried figuring it out from the documentation but to no avail.

bricklink API guide: https://www.bricklink.com/v3/api.page?page=general-notes

what i've tried:

import requests as r

headers = {
    "Authorization: OAuth realm":"",
    "oauth_consumer_key":"############",
    "oauth_token":"AC40C8C32A1748E0AE1EFA13CCCFAC3A",
    "oauth_signature_method":"HMAC-SHA1",
    "oauth_signature":"0IeNpR5N0kTEBURcuUMGTDPKU1c%3D",
    "oauth_timestamp":"1191242096",
    "oauth_nonce":"kllo9940pd9333jh",
    "oauth_version":"1.0" 
}

response = r.get("https://api.bricklink.com/api/store/v1/orders?/direction:in/", headers=headers)

print(response)

raise ValueError('Invalid header name %r' % (header,)) ValueError: Invalid header name b'Authorization: OAuth realm'

logan_9997
  • 574
  • 2
  • 14

1 Answers1

0

All of what they show on the page you linked is the value of the Authorization header, which you shouldn't generate yourself. Let OAuth1Session() generate it for you.

Use print_roundtrip() from https://stackoverflow.com/a/61803546 to see the request and response, and tweak your request if necessary.

import requests as r
import json
import textwrap
import sys
from requests_oauthlib import OAuth1Session

def print_roundtrip(response, *args, **kwargs):
    format_headers = lambda d: '\n'.join(f'{k}: {v}' for k, v in d.items())
    print(textwrap.dedent('''
        ---------------- request ----------------
        {req.method} {req.url}
        {reqhdrs}

        {req.body}
        ---------------- response ----------------
        {res.status_code} {res.reason} {res.url}
        {reshdrs}

        {res.text}
    ''').format(
        req=response.request, 
        res=response, 
        reqhdrs=format_headers(response.request.headers), 
        reshdrs=format_headers(response.headers), 
    ))


CONSUMER_KEY = "############"
CONSUMER_SECRET = "XYZ"
url = "https://api.bricklink.com/api/store/v1/orders?direction=in";

# see https://stackoverflow.com/a/55665510/8996878
oauthRequest = OAuth1Session(CONSUMER_KEY,
                    client_secret=CONSUMER_SECRET
                    )

response = oauthRequest.get(url, hooks={'response': print_roundtrip})
mustberuss
  • 140
  • 6