function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zachariah RosenbergZachariah Rosenberg 

REST API: Authentication by Session id

Hello,

I'm going through the Salesforce API REST guide, and I'm trying to authenticate via session id. My script is in python:

    import urllib, urllib2
    url = 'https://login.salesforce.com/services/oauth2/token'
    values = {
        "grant_type":"password",
        "client_id":"aaaaaaaaaaaa11111111111111",
        "client_secret":"1111111111111",
        "username":"myuser@domain.com",
        "password":"mypassword"
    }
    data = urllib.urlencode(values)
    req = urllib2.Request(url,data)
    sf = urllib2.urlopen(req)

I'm receiving a 400 Error:

    Traceback (most recent call last):
      File "/Users/ZR/Desktop/sf.py", line 14, in <module>
        sf = urllib2.urlopen(req)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 127, in urlopen
        return _opener.open(url, data, timeout)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 410, in open
        response = meth(req, response)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 523, in http_response
        'http', request, response, code, msg, hdrs)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 448, in error
        return self._call_chain(*args)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain
        result = func(*args)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 531, in http_error_default
        raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    urllib2.HTTPError: HTTP Error 400: Bad Request
    [Finished in 0.4s with exit code 1]

Where have I gone wrong?

Thank you! 
Best Answer chosen by Zachariah Rosenberg
HawkedHawked

Add the security token with the password, Looking  at the docs : 

 https://help.salesforce.com/apex/HTViewHelpDoc?id=remoteaccess_oauth_username_password_flow.htm&language=en (https://help.salesforce.com/apex/HTViewHelpDoc?id=remoteaccess_oauth_username_password_flow.htm&language=en)

There is a mention here that says :  

You must concatenate their password and token when passing the request for authentication.

All Answers

HawkedHawked

Add the security token with the password, Looking  at the docs : 

 https://help.salesforce.com/apex/HTViewHelpDoc?id=remoteaccess_oauth_username_password_flow.htm&language=en (https://help.salesforce.com/apex/HTViewHelpDoc?id=remoteaccess_oauth_username_password_flow.htm&language=en)

There is a mention here that says :  

You must concatenate their password and token when passing the request for authentication.
This was selected as the best answer
Zachariah RosenbergZachariah Rosenberg
I'm confused. I'm using the session id method to bypass using an oauth token. What I thought would happen is that this script would return a sesssion id that I could then use to authenticate my requests. 

Where do I get the security token from?
Zachariah RosenbergZachariah Rosenberg
Oh, and thanks for responding!
HawkedHawked
Login as the user who you are trying to authenticate, go to Setup--> My Personal Information --> Reset My security token, Make sure you have a valid email address on the user record. You should be getting the security token in an email.