phpoauth-2.0sugarcrm

SugarCRM fileDownload error after upgrade


I received this error after I upgraded to Sugar 7.8, when calling api.fileDownload():

{"error":"need_login","error_message":"No valid authentication for user."}

After some investigation, I figured out that Sugar upgraded the API calls for OAuth.

Following is my code:

 api.fileDownload(api.buildURL("Quotes/" + model.get("id") + "/pdf/download?OAuth-Token=" + api.getOAuthToken()), {
        success: function() {
            app.alert.show("pdf_download_api_success", {
                level: "success",
                messages: SUGAR.language.get('Quotes', 'LBL_QUOTE_PDF_GENERATED'),
                autoClose: true
            });
        },});

I checked the details in this URL, but I'm not able to add a header to the HTTPS request.


Solution

  • After so much research, I came up with a solution for this issue.

    Note: There is no supporting document for api.fileDownload( to use OAuth-token.

    So i tried using XMLHttpRequest and it worked fine.

    SOLUTION

        var request = new XMLHttpRequest();
        request.open('GET', api.buildURL("YOURMODULE/" + model.get("id") + "/pdf/download"), true);
        request.setRequestHeader('OAuth-Token', api.getOAuthToken()); // UR TOKEN
        request.responseType = "blob";
        request.onload = function (e) {
            if (this.status === 200) {
                // `blob` response
                // create `objectURL` of `this.response` : `.pdf` as `Blob`
                var file = window.URL.createObjectURL(this.response);
                var a = document.createElement("a");
                a.href = file;
                /*request.onreadystatechange = function() {
                  if(this.readyState == this.HEADERS_RECEIVED) {
                    console.log(request.getResponseHeader("Content-Type"));
                  }
                }*/
    
                a.download =  request.getResponseHeader("FileName");
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
            };
        };
        request.send();
    

    Check this thread may be in future there may be updates: https://community.sugarcrm.com/message/90474-re-sugarcrm-filedownload-error-after-upgrade?commentID=90474#comment-90474