benedwards44 / packagebuilder

Django + Heroku application for building a package.xml of your Salesforce Org
https://packagebuilder.herokuapp.com
MIT License
104 stars 81 forks source link

Call via API #4

Closed nvuillam closed 6 years ago

nvuillam commented 6 years ago

Hi, Is it possible to call your great tool via API ? I'd like to automatize via Jenkins the BackUp/Versionning of orgs , and your tool would help a lot , to get up to date package.xml then use it to do an ant Retrieve

Many thanks & best regards

Nicolas

benedwards44 commented 6 years ago

@nvuillam Yep good idea, I've done something similar for my Code Scanner app by enabling an API for that.

I will add this to my list and keep you updated.

nvuillam commented 6 years ago

That's great, many thanks ! :)

benedwards44 commented 6 years ago

Alrighty @nvuillam, the API is up and running. You can view the details http://packagebuilder.herokuapp.com and click the "API" button at the top of the page for details on how to use it.

Let me know if you have any questions.

nvuillam commented 6 years ago

I'm deeply sorry for the response delay, I haven't see that you already made it :)

Please forgive me for my newbiness ... but how do I get this access token ? I looked there and there and clicked everywhere in the setup but ... no way to find the clientId and clientSecret allowing me to login to SFDC to get this famous SFDC access token ...

Once I manage to do that, my groovy script calling your API seems quite ready and i'd be glad to share it in a MD/Wiki here , + your explanations about how to get this access Token of course

Many thanks & best regards :)

Nicolas

benedwards44 commented 6 years ago

@nvuillam no problem, Salesforce has actually made it a bit harder to reveal your access token because it's hidden from Debug Logs and no longer visible in Workbench.

You retrieve your access token (or Session ID) normally by logging into Salesforce via API (SOAP or REST), and your access token in returned in your authorisation call.

However, if you are already logged into Salesforce, using UserInfo.getSessionId() via Apex will return the Session Id to use.

Failing all that, you can create a simple VF page:

<apex:page showHeader="false">
    {!$Api.Session_ID}
</apex:page>
nvuillam commented 6 years ago

As I want your API to be called from a jenkinsFile managed script, i can't copy paste manually ^^

But I found some solution: create a connected app and activate OAuth2 login , then it generates clientId & clientSecret than can be used using such code : https://help.salesforce.com/articleView?id=connected_app_create.htm&type=5

    static loginToSFDC(String sf_login_domain,String consumer_key,String consumer_secret,String auth_username,String auth_password,String auth_security_token) {
        //Request Access_token and instance domain for work 
        def http = new HTTPBuilder(sf_login_domain)
        def postBody = [
            grant_type: 'password',
            client_id: consumer_key,
            client_secret: consumer_secret,
            username: auth_username,
            password: auth_password+auth_security_token
            ]
        def jsonResult ;
        try{ 

        http.post( path : 'services/oauth2/token',
                  body : postBody,
                  requestContentType: URLENC) { resp, json ->
                        println (resp);
                        println (json)
                        jsonResult = json ;
                        //access_token = json.access_token
                        //instance_domain = json.instance_url +"/"
                    }

        }catch(HttpResponseException e){
            println "Error code: ${e.statusCode}"
            println "Post form: $postBody \n"
            println e.getMessage();
        }

        println "Access Token : "+jsonResult.access_token
        println "Instance domain : "+jsonResult.instance_url
        return jsonResult ;
    }
benedwards44 commented 6 years ago

Yep, that's effectively logging in via REST API.

As an alternative, I can update my API to accept a username and password as an alternative to an accessToken, but I didn't really want the responsibility of accepting usernames and passwords.

nvuillam commented 6 years ago

I agree with you that it's not nice to access credentials. My code is soon ready, i'll clean it and share it for other newbies like me :)

nvuillam commented 6 years ago

I successfully called your api :) Cleaned code tomorrow probably, thanks again for your tool :)