0

I've been trying to translate some PHP code to Python 3 but can't quite get it to work. In PHP I have the following:

$request = "https://api.example.com/token";
$developerKey = "Basic VVVfdFdfsjkUIHDfdsjYTpMX3JQSDNJKSFQUkxCM0p0WWFpRklh";
$data = array('grant_type'=>'password',
                'username'=>'name',
                'password'=>'pass',
                'scope'=>'2346323');
$cjconn = curl_init($request);
curl_setopt($cjconn, CURLOPT_POST, TRUE);
curl_setopt($cjconn, CURLOPT_HTTPHEADER, array('Authorization: '.$developerKey));
curl_setopt($cjconn, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($cjconn, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($cjconn, CURLOPT_POSTFIELDS,http_build_query($data));
$result = curl_exec($cjconn);
curl_close($cjconn);
$tokens = json_decode($result,true);
$accesstoken = $tokens['access_token'];
echo $accesstoken."\n";

I tried converting it to the following in Python:

import pycurl, json

url = 'https://api.example.com/token'
data = json.dumps({"grant_type":"password",
                        "username":"name",
                        "password":"pass",
                        "scope":"2346323"})
key = 'Basic VVVfdFdfsjkUIHDfdsjYTpMX3JQSDNJKSFQUkxCM0p0WWFpRklh'
c = pycurl.Curl()
c.setopt(pycurl.URL,url)
c.setopt(pycurl.HTTPHEADER,['Authorization: {}'.format(key)])
c.setopt(pycurl.POST,1)
c.setopt(pycurl.POSTFIELDS,data)
c.perform()

But I get the following error:

<faultstring>String index out of range: -1</faultstring>

How can I correct this, or is there a more pythonic solution?

1
  • 1
    I'm trying to inquire what I am doing wrong. I've provided by attempt at solving a problem that is asked multiple times on SO, so it is clearly not well documented. Commented Apr 20, 2016 at 17:47

1 Answer 1

1

If anyone is interested in the solution, I came up with the following which worked:

    def getToken(self):
        """Retrieves the token from provider"""
        #The data to be passed to retrieve the token
        tokenData = {'grant_type':'password',
                     'username':TOKENUSERNAME,
                     'password':TOKENPASSWORD,
                     'scope':TOKENSCOPE}
        #The header parameters
        header_params = {'Authorization':KEY}

        #Make the request for the token
        r = requests.post(TOKENURL,data=tokenData,headers=header_params)
        #Check the status code
        if r.status_code not in [200,203]:
            self.log.logentry("There was an error retrieving the data from Linkshare: {}:{}".format(r.status_code,r.text))
            sys.exit()
        #Extract the data from the response
        data = r.json()
        #Parse the access token
        token = {'token':data['access_token'],
                 'type':data['bearer']}

        return token
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.