access token expire immediately or valid for one time

I am trying to fetch data from Sugar through API so after getting OAtuth token from sugar when I call an API e.g Accounts this works fine but after that, if I call another API with the same token it gives the error "invalid_grant" with a message "Your authentication token is invalid."

Parents
  • Hello Sunil

    I have the same problem a few months ago, I read the following guide: 

    support.sugarcrm.com/.../


    1 . Create a record in the OAuth Keys module:

    2 . In your nodejs app you can consume the sugar API, in the points of the REST API
    "/ ping" - support.sugarcrm.com/.../index.html

    "/ oauth2 / token" - support.sugarcrm.com/.../

    The following lines of code may help you:

    const autenticate = async function (customer_hash, id_platform) {
        let platform = readCustomPlatform(customer_hash, id_platform),
            result = false
    
        switch (platform.typePlatform) {
            case 'sugarcrm':
                if (platform.access_token) result = await sugarcrm_ping(platform)
                if (result) {
                    return platform.credentials.access_token
                }
                else {
                    result = await sugarcrm_getToken(platform)
                    if (result.access_token) {
                        platform.credentials.refresh_token = result.refresh_token
                        platform.credentials.download_token = result.download_token
                        platform.credentials.access_token = result.access_token
                        await updatePlatform(customer_hash, platform)
                        return result.access_token
                    }
                }
                return false
                break
        }
        return false
    }
    
    const sugarcrm_ping = async (platform) => {
        const response = await sendRequest({
            url: "/ping",
            baseURL: assambleUrl(platform),
            headers: {
                "Content-Type": "application/json",
                "oauth-token": platform.credentials.access_token
            }
        })
        if (!_.isBoolean(response)) {
            logger.debug("Success:", response.data + " token alive!")
            return true
        } else {
            return false
        }
    }
    
    const sugarcrm_getToken = async (platform, isRefresh) => {
        let body = {
            "client_id": platform.credentials.client_id,
            "client_secret": platform.credentials.client_secret
        }
    
        if (isRefresh) {
            body.grant_type = "refresh_token"
            body.refresh_token = platform.credentials.refresh_token
        } else {
            body.grant_type = "password"
            body.username = platform.credentials.username
            body.password = platform.credentials.password
            body.platform = platform.credentials.platform
        }
        const response = await sendRequest({
            url: "/oauth2/token",
            baseURL: assambleUrl(platform),
            method: 'post',
            headers: { "Content-Type": "application/json" },
            data: body
        })
    
        if (!_.isBoolean(response)) {
            return response.data
        } else {
            return response
        }
    }
    
    

    All the parameters are stored in a config.json

     

    {
    "domain": "qa.crm.com",
    "isSecure": true,
    "credentials": {
                "username": "",
                "password": "",
                "client_id": "",
                "client_secret": "",
                "platform": "opi",
                "refresh_token": "",
                "download_token": "",
                "access_token": ""
              },
              "versionAPI": "v11_9"
    }

    Values for

    "client_id": "",
    "client_secret": ""

    extract them from this config

    Kind regards.

Reply
  • Hello Sunil

    I have the same problem a few months ago, I read the following guide: 

    support.sugarcrm.com/.../


    1 . Create a record in the OAuth Keys module:

    2 . In your nodejs app you can consume the sugar API, in the points of the REST API
    "/ ping" - support.sugarcrm.com/.../index.html

    "/ oauth2 / token" - support.sugarcrm.com/.../

    The following lines of code may help you:

    const autenticate = async function (customer_hash, id_platform) {
        let platform = readCustomPlatform(customer_hash, id_platform),
            result = false
    
        switch (platform.typePlatform) {
            case 'sugarcrm':
                if (platform.access_token) result = await sugarcrm_ping(platform)
                if (result) {
                    return platform.credentials.access_token
                }
                else {
                    result = await sugarcrm_getToken(platform)
                    if (result.access_token) {
                        platform.credentials.refresh_token = result.refresh_token
                        platform.credentials.download_token = result.download_token
                        platform.credentials.access_token = result.access_token
                        await updatePlatform(customer_hash, platform)
                        return result.access_token
                    }
                }
                return false
                break
        }
        return false
    }
    
    const sugarcrm_ping = async (platform) => {
        const response = await sendRequest({
            url: "/ping",
            baseURL: assambleUrl(platform),
            headers: {
                "Content-Type": "application/json",
                "oauth-token": platform.credentials.access_token
            }
        })
        if (!_.isBoolean(response)) {
            logger.debug("Success:", response.data + " token alive!")
            return true
        } else {
            return false
        }
    }
    
    const sugarcrm_getToken = async (platform, isRefresh) => {
        let body = {
            "client_id": platform.credentials.client_id,
            "client_secret": platform.credentials.client_secret
        }
    
        if (isRefresh) {
            body.grant_type = "refresh_token"
            body.refresh_token = platform.credentials.refresh_token
        } else {
            body.grant_type = "password"
            body.username = platform.credentials.username
            body.password = platform.credentials.password
            body.platform = platform.credentials.platform
        }
        const response = await sendRequest({
            url: "/oauth2/token",
            baseURL: assambleUrl(platform),
            method: 'post',
            headers: { "Content-Type": "application/json" },
            data: body
        })
    
        if (!_.isBoolean(response)) {
            return response.data
        } else {
            return response
        }
    }
    
    

    All the parameters are stored in a config.json

     

    {
    "domain": "qa.crm.com",
    "isSecure": true,
    "credentials": {
                "username": "",
                "password": "",
                "client_id": "",
                "client_secret": "",
                "platform": "opi",
                "refresh_token": "",
                "download_token": "",
                "access_token": ""
              },
              "versionAPI": "v11_9"
    }

    Values for

    "client_id": "",
    "client_secret": ""

    extract them from this config

    Kind regards.

Children
No Data