How do i fix "Login information disallowed" error from google script?

Viewed 1924

I'm trying to create products in Shopify with their API through a google sheet. Whenever I try to create an HTTP request I get the following error:

"Login information disallowed"

To test the API, I created a more or less identical python script. When I run the Python script the product gets created as planned. But I just can't get it to work through my google sheet.

GOOGLE SCRIPT CODE (NOT WORKING) :

function create_product() {

    var url = "https://XXXX:XXXX@XXXX.myshopify.com/admin/api/2019-04/products.json";

    var data = {
             "product": {
                 "title": 'test 1',
                 "body_html": 'test 1',
                 "vendor": 'test 1',
                 "product_type": 'test 1',
                 "tags" : 'test 1'
                  }
              };

    var params = {
             method : 'POST',
             contentType : 'application/json',
             payload : JSON.stringify(data),
             };

    var post = UrlFetchApp.fetch(url, params);
}

PYTHON CODE:

import requests

url = "https://XXXX:XXXX@XXXX.myshopify.com/admin/api/2019-04/products.json"

data = {
        "product": {
             "title": 'test 1',
             "body_html": 'test 1',
             "vendor": 'test 1',
             "product_type": 'test 1',
             "tags" : 'test 1'}
        }

post = requests.post(url=url, json=data)

I expected both scripts to give me the same results, but only the Python script seems to work. The "Login information disallowed" error keeps showing up in the google script, when i try to do the http post.

2 Answers

As per this Stack Overflow answer and this issue on Google bug tracker, API credentials in the URL does not work well. Consider adding them in Authorization header. A sample code for POST request will be like

var response = UrlFetchApp.fetch(url, {
            "method": "post",
            'contentType': 'application/json',
            'payload': JSON.stringify(payload),
            "headers": {
                "Authorization": "Basic " + Utilities.base64Encode("username_here:password_here")
                }
            });

I had the same issue, I was able to fix it with the followings:

Here is what my Python code was:

    useremail= ""
    userpass =""
    SomeOtherKey_Value =""
    login_url_auth =""
    login_url = ""
    payload  = {

    payload = {"email":useremail,"password":userpass,"SOME OTHER KEY(case sensistive!!!)":SomeOtherKey_Value}

    result = session_requests.post(

        login_url_auth, 
        data = json.dumps(payload ),
        headers = {"referer":login_url, "Content-Type":"application/json"}
    )

Here is my current GoogleScript code:

useremail= ""
    userpass =""
    SomeOtherKey_Value =""
    login_url_auth =""
    login_url = ""

    var data = {
    'email': useremail,
    'password': userpass,
    'SOME OTHER KEY(case sensistive!!!)':SomeOtherKey_Value
    };

    var options = {
        'method' : 'post',
        'contentType': 'application/json',  // other post it was Content_Type!! no it's contentType for my case at least, even though Chrome developer mode shows it as Content_Type.

        'referer': login_url,
        // Convert the JavaScript object to a JSON string.
        // other post it was body!! no it's payload for my case at least, even though Chrome developer mode shows it as body 
        'payload' : JSON.stringify(data)
    };

    var result = UrlFetchApp.fetch(login_url_auth , options);

Once i was able to fix the request call, here is how I was able to check the result.

 var result = UrlFetchApp.fetch(login_url_auth , options);

    for(i in result) {
        Logger.log(i + ": " + result[i]);
        //Logger.log("Data " + ": " + result[i].getContent());
    }
    Logger.log("Data " + result.getContentText())

Other source: https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app

Related