Trouble with simple https authentication with urllib2 (to get PayPal OAUTH bearer token)
Question: I'm at the first stage of integrating our web app with PayPal's express
checkout api. For me to place a purchase, I have to get a Bearer token of
course using our client id and our client secret.

I use the following curl command to successfully get that token:

    
    
    curl https://api.sandbox.paypal.com/v1/oauth2/token \
    -H "Accept: application/json" \
    -H "Accept-Language: en_US" \
    -u "ourID:ourSecret" \
    -d "grant_type=client_credentials"
    

Now I am trying to achieve the same results in python using urllib2. I've
arrived at the following code, which produces a 401 HTTP Unauthorized
exception.

    
    
        import urllib
        import urllib2
    
        url = "https://api.sandbox.paypal.com/v1/oauth2/token"
    
        PAYPAL_CLIENT_ID = "ourID"
        PAYPAL_CLIENT_SECRET = "ourSecret"
    
        passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
        passman.add_password(None, url, PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET)
        authhandler = urllib2.HTTPBasicAuthHandler(passman)
        opener = urllib2.build_opener(authhandler)
        urllib2.install_opener(opener)
    
        req = urllib2.Request( url=url,
          headers={
                "Accept": "application/json",
                "Accept-Language": "en_US",
                },
          data =urllib.urlencode({
                "grant_type":"client_credentials",
                }),)
    
        result = urllib2.urlopen(req).read()
        print result
    

Does anyone have any idea what I'm doing wrong above? Many thanks for any
insights


Answer: Experiencing the same problem here. Based on [Get access token from Paypal in
Python - Using urllib2 or requests
library](http://stackoverflow.com/questions/31476540) working python code is:

    
    
    import urllib
    import urllib2
    import base64
    token_url = 'https://api.sandbox.paypal.com/v1/oauth2/token'
    client_id = '.....'
    client_secret = '....'
    
    credentials = "%s:%s" % (client_id, client_secret)
    encode_credential = base64.b64encode(credentials.encode('utf-8')).decode('utf-8').replace("\n", "")
    
    header_params = {
        "Authorization": ("Basic %s" % encode_credential),
        "Content-Type": "application/x-www-form-urlencoded",
        "Accept": "application/json"
    }
    param = {
        'grant_type': 'client_credentials',
    }
    data = urllib.urlencode(param)
    
    request = urllib2.Request(token_url, data, header_params)
    response = urllib2.urlopen(request).open()
    print response
    

The reason, I believe, is explained at [Python urllib2 Basic Auth
Problem](http://stackoverflow.com/questions/2407126)

> Python libraries, per HTTP-Standard, first send an unauthenticated request,
> and then only if it's answered with a 401 retry, are the correct credentials
> sent. If the servers don't do "totally standard authentication" then the
> libraries won't work.

