аутентифицироваться до aws с топором ios и aws4 - PullRequest
1 голос
/ 06 марта 2020

Я пытаюсь пройти аутентификацию на AWS для отправки запроса на AWS Полли из javascript кода в редакторе WordPress. Я использовал несколько примеров для inte rnet, но всегда получаю ошибки.

const CREDS = {
      accessKeyId: "xxx",
      secretAccessKey: "xxx"
      // region: "eu-west-1"
    }; 

  axios(
      aws4.sign(
        {
          host: "polly.eu-west-1.amazonaws.com",
          method: "GET",
          url:
            "https://polly.eu-west-1.amazonaws.com/v1/voices?Engine=neural&IncludeAdditionalLanguageCodes=no&LanguageCode=en-US",
          data: {},
          body: {},
          path:
            "/v1/voices?Engine=neural&IncludeAdditionalLanguageCodes=no&LanguageCode=en-US"
        },
        CREDS
      )
    ).then(res => {
      console.log(res);
      // ...
    });

xhr. js: 126 Отказался устанавливать небезопасный заголовок "Host" setRequestHeader @ xhr. js: 126 forEach @ utils. js: 238 dispatchXhrRequest @ xhr. js: 120 xhrAdapter @ xhr. js: 12 dispatchRequest @ dispatchRequest. js: 52 Promise.then (asyn c) запрос @ Ax ios. js: 61 wrap @ bind. js: 9 (анонимный) @ edit. js: 88 Vh @ реагировать-dom.min. js? ver = 16,9. 0: 163 Uh @ реагирующий-дом.мин. js? Ver = 16,9,0: 14 Xh @ реагирующий-дом.мин. js? Ver = 16,9,0: 14 af @ реагирующий-дом.мин. js? Ver = 16.9.0: 14 Yh @ реаги-дом.мин. js? Ver = 16.9.0: 164 nd @ реаги-дом.мин. js? Ver = 16.9.0: 15 н c @ реаги-дом.мин. js? Ver = 16,9,0: 15 из @ реагировать-дом.мин. js? Ver = 16,9,0: 38 Ac @ реагировать-дом.мин. js ? ver = 16.9.0: 39 unstable_runWithPriority @ реагир.мин. js? ver = 16.9.0: 26 млн. @ реагировать-дом.мин. js? ver = 16.9.0: 52 Be @ реагировать-дом. мин. js? ver = 16,9,0: 119 xi @ реаги-дом.мин. js? ver = 16,9,0: 39 xhr. js: 126 Отказался установить небезопасный заголовок «Content-Length» setRequestHeader @ xhr. js: 126 forEach @ utils. js: 238 dispatchXhrRequest @ xhr. js: 120 xhrAdapter @ xhr. js: 12 dispatchRequest @ dispatchRequest. js: 52 Promise.then (asyn c) request @ Ax ios. js: 61 wrap @ bind. js: 9 (анонимный) @ edit. js: 88 Vh @ реагировать на дом. мин. js? ver = 16.9.0: 163 Uh @ реаги-дом.мин. js? ver = 16.9.0: 14 Xh @ реаги-дом.мин. js? ver = 16.9.0: 14 af @ response-dom.min. js? Ver = 16.9.0: 14 Yh @ реаги-dom.min. js? Ver = 16.9.0: 164 nd @ реагировать-dom.min. js ? ver = 16.9.0: 15 n c @ реаги-дом.мин. js? ver = 16.9.0: 15 Of @ реаги-дом.мин. js? ver = 16.9.0: 38 А c @ react-dom.min. js? Ver = 16.9.0: 39 unstable_runWithPriority @ реаги.мин. js? Ver = 16.9.0: 26 млн. Долл. @ Response-dom.min. js? ver = 16.9.0: 52 Be @ реагирующий-дом.мин. js? ver = 16.9.0: 119 xi @ реагирующий-дом.мин. js? ver = 16.9.0: 39 xhr. js : 178 GET https://polly.eu-west-1.amazonaws.com/v1/voices?Engine=neural&IncludeAdditionalLanguageCodes=no&LanguageCode=en-US 403 (запрещено)

и с этим

      aws4.sign(
        {
          service: "polly",
          region: "eu-west-1",
          method: "GET",
          path:
            "/v1/voices?Engine=neural&IncludeAdditionalLanguageCodes=no&LanguageCode=en-US",
          headers: {},
          body: "{}"
        },
        CREDS
      )
    ).then(res => {
      console.log(res);
      // ...
    });

isURLSameOrigin. js: 57 Uncaught (в обещании) TypeError: Невозможно прочитать свойство 'protocol' undefined в isURLSameOrigin (isURLSameOrigin. js: 57) в dispatchXhrRequest (xhr. js: 109) в новом Promise () в xhrAdapter ( xhr. js: 12) at dispatchRequest (dispatchRequest. js: 52)

Я не понимаю, почему это так сложно. Почему я делаю не так?

1 Ответ

0 голосов
/ 16 марта 2020

AWS4 причиняла мне головную боль в течение нескольких дней! Я нашел решение, используя вместо этого усиление. Создайте правильные заголовки

import {Signer} from '@aws-amplify/core';

let request = {         
        method: 'GET',
        url: 'https://polly.eu-west-1.amazonaws.com/v1/voices?Engine=neural&IncludeAdditionalLanguageCodes=no&LanguageCode=en-US',
        data: '' 
    } 
    let access_info = {
        access_key: xxxxx, 
        secret_key: xxxxxx,
        session_token: xxxxx
    }
    let service_info = {
        service: 'polly',
        region: 'eu-west-1'
    }


    //use amplify sign()function to create the signed headers;
    let signedRequest =  Signer.sign(request,access_info,service_info)

    //remove host from header
    delete signedRequest.headers['host']

    //I normally create an instance if I need to intercept my response or request
    var instance = axios.create();


    let response = await instance(signedRequest).then(function (response) {    
       console.log(response);
       return response

     }).catch(function (error) {

         //... handle errors

     });

Надеюсь, это поможет вам

...