【问题标题】:Authorization Header missing on POST request [duplicate]POST请求中缺少授权标头[重复]
【发布时间】:2020-11-12 23:04:19
【问题描述】:

我正在尝试使用 fetch(). 向 URL 发送 POST 请求

我故意设置了 Authorization 标头,但是当我在 Firefox 开发工具中检查响应时,它输出以下错误 "Missing request header 'Authorization' for method parameter of String"

        var target_url = "https://api.sonos.com/login/v3/oauth/access";
        var encoded_msg = btoa(client_id + ':' + secret); // base64-encodes client_id and secret using semicolon as delimiter
        var params = `grant_type=authorization_code` + `&code=${authCode}` + `&redirect_uri=${redirect_uri}`;
        var myHeaders = new Headers({
            'Authorization': `Basic ${encoded_msg}`,
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'POST',
            'Content-Length': params.length,
            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
        });
    
        fetch(target_url, {
            method: 'POST',
            mode: 'no-cors',
            credentials: 'include',
            redirect: 'follow',
            headers: myHeaders,
            body: params
        })
        .then(response => {
            console.log("Status: " + response.status);
            console.log("StatusText: " + response.statusText);
            console.log("Type: " + response.type);
            console.log("URL: " + response.url);
        });

什么会删除 Authorization-Header,为什么以及如何防止它?

编辑:
为了澄清,我正在使用 Firebase Cloud Functions 托管我的网页,我从该网页将请求发送到 Sonos Authorization API
使用 Postman,请求通过,我得到了正确的响应。

【问题讨论】:

  • 嗨,我知道你很新,但我一直站在你的立场上。这个问题的表述很好,但你迷路了,不知道去哪里找。为了回答您的问题...我不知道您的设置,所以我无法判断。但是我可以告诉你的是使用邮递员进行调试(postman.com),我要做的是从邮递员发送请求以查看错误是在服务器中还是在您的客户端中,如果您使用邮递员发送请求带有授权标头,然后是服务器问题,请联系服务器人员
  • 您需要添加 Bearer 而不是 Basic。这是 Basic 故意的吗?
  • 然后我会尝试查看请求,调试它。尝试使用 console.log(this),在发送之前查看您发送的所有参数
  • 承载或基本取决于服务器配置,我目前正在处理基本并且它有效
  • 我以前在这里遇到过这样的问题,最后写了this article about no-cors你可能会觉得有帮助!

标签: javascript http sonos


【解决方案1】:

您为检索访问令牌执行的步骤必须在云函数端点中执行。

获取访问令牌
获得授权码后,使用它来获取访问令牌和刷新令牌。使用访问令牌通过 Sonos 云向家庭发送 API 调用。此步骤使用您的客户端密码,因此它应该是服务器端请求,而不是客户端(基于浏览器)请求。

参考: https://developer.sonos.com/build/direct-control/authorize/

package.json 中引入node-fetch 作为依赖项,因为它的API 实现与浏览器获取密切相关。

如下添加端点:


const fetch = require('node-fetch');
const functions = require('firebase-functions');

const secret = functions.config().sonos.secret;
const client_id = functions.config().sonos.client_id;
const redirect_uri = functions.config().sonos.redirect_uri;

exports.retrieveAccessToken = functions.https.onRequest(async (req, res) => {
        const {authCode} = req.query;
        const target_url = "https://api.sonos.com/login/v3/oauth/access";
        const encoded_msg = btoa(`${client_id}:${secret}`); // base64-encodes client_id and secret using semicolon as delimiter
        const body = `grant_type=authorization_code&code=${authCode}&redirect_uri=${redirect_uri}`;
        const headers = new Headers({
            'Authorization': `Basic ${encoded_msg}`,
            'Content-Length': body.length,
            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
        });
    
        const response = await fetch(target_url, {
            method: 'POST',
            redirect: 'follow',
            headers,
            body
        });

       const token_data = await response.json();
       return token_data;
});

修改网页中的代码,在用户从Sonos登录服务返回后向云函数端点发出请求。

 
 const authCode = new URLSearchParams(window.location.search).get('code');

 fetch(`https://us-central1-<project-id>.cloudfunctions.net/retrieveAccessToken?authCode=${code}`);

【讨论】:

    猜你喜欢
    • 2014-12-16
    • 2018-07-17
    • 2017-12-19
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 2011-05-04
    • 2012-10-27
    相关资源
    最近更新 更多