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
Singer-ClarkSinger-Clark 

Logging in SOAP API / Metadata API in Python

I'm using Python [zeep](http://docs.python-zeep.org/en/master/) to use SOAP to interact with Salesforce's Metadata API.

I'm attempting to start with the simplest thing possible, just log in and read a metadata object.


    from zeep import Client as zClient
    
    client = zClient('sf_config/sf_metadata_40.xml')
    
    // readMetadata method exists, prints <zeep.client.OperationProxy object at ...>
    print client.service.readMetadata

    // errors because login method does not exist
    print client.service.login

I know the zeep client reads the XML because it correctly knows that `readMetadata` exists, but I'm confused about how I am supposed to log in with my Salesforce username and password. I expected a `login` method to exist.

Additionally, I tried adding username and password to the construction of the `client` object:

    from zeep.wsse.username import UsernameToken

    client = zClient('sf_config/sf_metadata_40.xml',
                     wsse=UsernameToken('bub@bob.com', 'scary_secretPassW*rd'))

but it was unsuccessful, in that, executing the `readMetadata` method caused this error:  
`Fault: UNKNOWN_EXCEPTION: Destination URL not reset. The URL returned from login must be set in the SforceService`

(The Java examples in Salesforce docs use a `connection` object that I'm not really sure what the Python zeep equivalent would be, so I don't have an example to go off of.)

UPDATE:  
That error brings this to mind, from the [docs](https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_calls_intro.htm):
> Retrieve the metadataServerUrl from the LoginResult returned by your SOAP API login() call.

But I'm not sure what the `login()` call is, since such a method does not exist on the `client.service`.
Suraj PSuraj P
The Metadata API doesn't have a login method of its own. You have to login using the Partner or Enterprise WSDL and then just use the session token and server url retrieved to make calls using the Metadata API. Set the SessionId in the header and the server url as the endpoint before making calls using the metadata api.
Singer-ClarkSinger-Clark
Thanks for the response! Follow-up question now that I know (from these docs: https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_quickstart_java_sample.htm) one can use OAuth flow instead of username/password login:

That says I can use access token from OAuth flow (which I've already done before, so have the access token) in place of the session ID, which you mention to put in the header.

But what about the server URL? How can I get that if I'm using the OAuth flow (and thus don't do the login() call which normally gives you the server URL to use as the endpoint)?
Suraj PSuraj P
The OAuth flow (Web Server Flow and User-Agent Flow) returns instance_url as one of the parameters. Please refer to the following link for details:
https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com
 
Singer-ClarkSinger-Clark
Ah, cool, I have seen this instance_url you speak of.

How does that relate to Metadata API though? Once you have that, is there a specific URL path to put at the end of it, to construct the Metadata API?
Singer-ClarkSinger-Clark
From that Link you gave, it looks like the example Metadata API server URL ("https://na1.salesforce.com/services/Soap/m/{version}/00D50000000IZ3Z") depends on instance_url (the "https://na1.salesforce.com" part) and some ID (the "00D50000000IZ3Z" part). But doesn't say where to get that ID. I would have guessed that it's the organization ID, but the object also includes the organization_id ("00D50000000IZ3ZEAW") and it doesn't equal what's in the URL.
Suraj PSuraj P
"00D50000000IZ3ZEAW" and "00D50000000IZ3Z" are both the same Id. The former is the 18 digit case-safe Salesforce ID. 
https://help.salesforce.com/articleView?id=000004383&type=1
 
Singer-ClarkSinger-Clark
Oooh, cool. Thank you!
Suraj PSuraj P
Can you please mark my answer as "Best Answer" i it helped you? Thanks!