我正在连接到Shopify API,但是它的访问结构是apikey:password@storename.myshopify.com
但是,Apps脚本不允许您使用带有UrlFetchApp.fetch函数12345:67890@storename.myshopify.com的登录凭据的URL,而且我似乎无法通过基本标头成功传递信息。
ViewTreeObserver viewTreeObserver = myView.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
//make Toast
}
当我记录来自响应的输出时,看来我正在进入登录HTML,但是从未超越。
在阐明这种访问方法方面的任何帮助将不胜感激。
记录器响应:
function shopify_connect() {
var url = "https://storename.myshopify.com/admin/products.json/GET";
var apikey = '12345';
var password = '67890';
var headers = {
"User-Agent" :"info@website.com",
"Authentication" : 'Basic ' + Utilities.base64Encode(apikey + ':' + password)
}
var options = {
"headers": headers,
}
var response = UrlFetchApp.fetch(url, options);
}
答案 0 :(得分:2)
因此,在进一步处理它之后,再看看引用的Cooper示例,我就能够正确进行连接处理。
以下是将来其他人遇到相同问题的代码:
function shopify_connection_test() {
var api_key = '<enter api key here';
var api_pass = '<enter api password here>';
var store_url = 'https://storename.myshopify.com/admin/products.json';
var product = {
"product": {
"title": "Burton Custom Freestyle 151",
"body_html": "<strong>Good snowboard!</strong>",
"vendor": "Burton",
"product_type": "Snowboard",
"tags": "Barnes & Noble, John's Fav, \"Big Air\""
}
}
var payload = JSON.stringify(product);
var headers = {
"Content-Type" : "application/json",
"Authorization": "Basic " + Utilities.base64Encode(api_key + ":" + api_pass)
};
var params = {
"method" : "POST",
"headers" : headers,
"contentType" : "application/json",
"payload" : payload
}
var response = UrlFetchApp.fetch(store_url, params)
Logger.log(response.getContentText())
}
这是专门上传产品,我不想排除任何信息。对于希望更改其整体功能的任何人,Shopify API文档中的引用应该足够。
答案 1 :(得分:0)
我遇到了类似的问题,即使在GAS上使用UrlFetchApp时,Shopify也会返回(401)未经授权的{“错误”:“ [API]无效的API密钥或访问令牌(无法识别的登录名或错误的密码)”}}该请求与Postman完美配合。
Runeworks Gaming解决方案有效,其窍门是将授权传递到嵌套在url获取应用程序“参数”中的“标头”对象中。
在很多其他场合,我都通过将“ Authorization”标头直接放入参数中来使用UrlFetchApp,但是显然,这不适用于Shopify。
工作:
var SHOP_ID = "Your shop ID";
var API_KEY = "Your API key";
var API_PASSWORD = "Your API password";
var encoded = Utilities.base64Encode(API_KEY + ':' + API_PASSWORD);
var headers = {
"Content-Type" : "application/json",
"Authorization": "Basic " + encoded
};
var options = {
"contentType" : "application/json",
'method' : 'GET',
'headers' : headers, // This is the important part
'followRedirects' : false,
};
var response = UrlFetchApp.fetch("https://" + SHOP_ID + ".myshopify.com/admin/api/2019-04/orders.json?status=any",options);
var RESPONSE_CODE = response.getResponseCode();
var CONTENT_JSON = JSON.parse(response.getContentText());