0

基于教程,我尝试了以下代码。我正在尝试向网页添加一个新脚本。

request.post(accessTokenRequestUrl, {
      json: accessTokenPayload
  })
  .then((accessTokenResponse) => {
          const accessToken = accessTokenResponse.access_token;
          // DONE: Use access token to make API call to 'shop' endpoint
          const shopRequestUrl = 'https://' + shop + '/admin/shop.json';
          const shopRequestHeaders = {
              'X-Shopify-Access-Token': accessToken,
              'Content-Type': 'application/json'
          };


          const createScriptTagUrl = 'https://' + shop + '/admin/script_tags.json';
          const scriptTagBody = {
              "script_tag": {
                  "event": "onload",
                  "src": "https:\/\/djavaskripped.org\/fancy.js"
              }
          }

          request.get(shopRequestUrl, {
                  headers: shopRequestHeaders
              })
              .then((shopResponse) => {
                  res.status(200).end(shopResponse);
              })
              .catch((error) => {
                  res.status(error.statusCode).send(error.error.error_description);
              });
          request.post(createScriptTagUrl, {
                  json: scriptTagBody
              }, {
                  headers: shopRequestHeaders
              })
              .then((scriptResponse) => {
                  res.status(200).end(scriptResponse);
              })
              .catch((error) => {
                  res.status(error.statusCode).send(error.error.error_description);
              });

但是,我得到RequestError: Error: Invalid URI "/"

我错过了什么吗?还是 src 值有问题?

4

2 回答 2

0

I think you are using get method to create the script tag instead of post. Please use post method and also remove \ from the src.

Thanks

于 2017-11-15T09:40:29.097 回答
0

使用以下代码修复。基本上,请求正文应该以 JSON 格式发送。

request.post({
    url: createScriptTagUrl,
    body: scriptTagBody,
    headers: shopRequestHeaders,
    json: true
}, function(error, response, body) {
    if (!error) {
        console.log(body)
    }
});
于 2017-11-17T04:22:00.863 回答