我正在尝试通过Google表格在Shopify中使用其API创建产品。每当我尝试创建HTTP请求时,都会出现以下错误:
“不允许登录信息”
为了测试API,我创建了一个或多或少相同的python脚本。当我运行Python脚本时,产品将按计划创建。但我只是无法通过我的Google工作表来工作。
GOOGLE脚本代码(不工作):
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代码:
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)
我希望这两个脚本都能给我相同的结果,但是似乎只有Python脚本有效。当我尝试执行http帖子时,“不允许登录信息”错误一直显示在Google脚本中。
答案 0 :(得分:0)
根据此Stack Overflow答案和Google Bug跟踪程序上的issue,URL中的API凭据无法正常运行。考虑将它们添加到Authorization标头中。 POST请求的示例代码如下
var response = UrlFetchApp.fetch(url, {
"method": "post",
'contentType': 'application/json',
'payload': JSON.stringify(payload),
"headers": {
"Authorization": "Basic " + Utilities.base64Encode("username_here:password_here")
}
});
答案 1 :(得分:0)
我遇到了同样的问题,可以通过以下方法解决此问题:
这是我的Python代码:
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"}
)
这是我当前的GoogleScript代码:
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);
一旦我能够解决请求调用,这就是我能够检查结果的方式。
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())
其他来源:https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app