首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法解码Amazon Advertising API下载的报告

无法解码Amazon Advertising API下载的报告
EN

Stack Overflow用户
提问于 2020-01-28 21:19:03
回答 1查看 466关注 0票数 1

我有关于亚马逊广告api的问题。我正在使用nodejs库(https://github.com/humanstupidity/amazon-advertising-api)从亚马逊获取一个报告,它返回给我一个似乎是二进制格式的主体,但我无法使其在JSON中可读。

我在postman中尝试了该请求,它返回相同的二进制响应。我必须从postman下载并解压缩该文件以使其可读,但不知何故我无法使其在nodeJS中工作。

这是库中重要的部分:

代码语言:javascript
复制
const request = require('request');
const util = require('util');

const requestPromise = util.promisify(request);
const postRequestPromise = util.promisify(request.post);
const getRequestPromise = util.promisify(request.get);


module.exports = class AdvertisingClient {
    async getReport(reportId) {

        while (true) {

            let reportRequest = JSON.parse(await this._operation(`sp/reports/${reportId}`));

            if (reportRequest.status === 'SUCCESS') {
                return this._download(reportRequest.location);
            }

            await sleep(500);

        }

    }
    _download(location, auth = true)
    {
        let headers = {}

        if (auth) {
            headers.Authorization = 'Bearer ' + this.options.accessToken;
        }

        if (this.options.profileId) {
            headers['Amazon-Advertising-API-Scope'] = this.options.profileId;
        }

        let requestOptions = {
            url: location,
            headers: headers,
            followRedirect: false,
            gzip: true
        }

        return this._executeRequest(requestOptions);
    }

    async _executeRequest(requestOptions){

        let response = await getRequestPromise(requestOptions);

        if (response.statusCode == 307) {
            let finalResult = await this._download(response.headers.location, false);
            return finalResult;
        }

        let result={
            success: false,
            code: response.statusCode,
            response: response.body,
            requestId: 0
        };

        console.log(result);

        if (!(response.statusCode < 400 && response.statusCode >= 200)) {
            let data = JSON.parse(response.body);

            if (data && data.requestId) {
                requestId = data.requestId;
            }

            result = false;
        } else {
            result.success = true;
        }

       return result

    }

    async _operation(_interface, data, method = 'GET') {
        let url = `https://${this.endpoint}/v2/${_interface}`;
        let headers = {
            'Authorization': 'Bearer ' + this.options.accessToken,
            'Amazon-Advertising-API-ClientId': this.options.clientId,
            'Content-Type': 'application/json',
        }

        if (this.options.profileId) {
            headers['Amazon-Advertising-API-Scope'] = this.options.profileId;
        }

        let requestOptions={
            url: url,
            headers: headers,
            method: method,
            gzip: true,
        }

        if (method=="GET") {
            requestOptions.qs=data;
        }else{
            requestOptions.json=data;
        }

        let response = await requestPromise(requestOptions)

        let resData = response.body;

        if (!resData.error) {
            return resData;
        } else {
            throw resData.error;
        }

    }
}

我通过以下函数调用代码:

代码语言:javascript
复制
let reportResult = await client.getReport(reportID);

这就是我得到的结果:

代码语言:javascript
复制
{
  success: false,
  code: 200,
  response: '\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000���j�@\u0010\u0006�{�B<�\u0010�������P�\u0016�[���M�F\r\u001a\u000b����\u0002y�nӖ�!�7����7�O\u001faRT�p�Fa\u0012g�8}�g�p\u0002H\\\u0019�H�����.M^��4ۗ���"�Z�\u0016q��I8��MR:��y3�\u001a/�F�P3\u0004�C�\u0017�t\\\u0017Y\u001a��\u001b��v��Hf��(`��0�+֧�g�8��]\u000b\u0019\u0013�\u001d-I\u001b.%��g��`���$���\u001f(��f~�tʐ�`H�/C���\u0011�>\u0014M\u0000�Fw\f��\u001b\u0018���\f�71`.���]Ev9[4\r1��5��!�˥�i\u0018�m�\u001c�R�=3��I�VL(����t�~sm_��\\i!\u0005\n' +
    '�٠�aU���=��e�\u0007KW�Ypk�z(��Q��\u0003\u0013$`�em\u0010�\u0018=�d�����}���y3��\u000b.��=9\u0004\u0000\u0000',
  requestId: 0
}

我也使用了zlib,但只得到了错误Error: unexpected end of file

我是nodeJS的新手,真的不知道问题出在哪里。

非常感谢您的帮助!

EN

回答 1

Stack Overflow用户

发布于 2020-10-10 18:04:12

body是一个gzip'ed (压缩的) JSON,你需要先gunzip它。

如果你使用amazon-advertising-api-sdk包,它会自动为你做这件事。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59949688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档