Particle Photon API:获取客户令牌适用于 Postman 但不适用于 axios

雅各布

我正在开发一个结合 Photon Particle 的 React Native 应用程序。通过遵循双腿身份验证的文档在配置设备之前,我需要获取索赔代码。

curl -X POST \
  https://api.particle.io/oauth/token \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'grant_type=client_credentials&client_id=client_id&client_secret=clsecret&scope=customer%3Demail%40gmail.com'

当我使用 CURL 甚至邮递员执行请求时,我得到了想要的结果。但是当我在 react native (iOS) 中使用 axios 尝试这个时,我总是收到以下错误:Invalid or missing grant_type parameter.

下面的代码是我检索数据的 React Native 代码。正如你所看到的,我正在传递 grant_type。

costumerToken() {
    const route = `${this.route}/oauth/token`;
    const headers = {
        "Accept": "application/json",
        "Content-Type": "application/x-www-form-urlencoded"
    }
    const body = {
        "grant_type": "client_credentials",
        "client_id": this.clientId,
        "client_secret": this.clientSecret,
        "scope": `customer=${this.costumerEmail}`
    }
    console.log(route, headers, body);
    return axios.post(route, body, {headers: headers})
        .then(res => {
            return Promise.resolve(res);
        })
        .catch(err => {
            return Promise.reject(err.response);
        });
}

怎么了?

须田

传递Objectasaxios.post()正文时,它会将其作为 JSON 发送,但粒子 API 期望它是application/x-www-form-urlencoded. Axios 文档更深入地探讨了这个主题。为了使其工作,您可以将代码更改为:

customerToken() {
    const route = `${this.route}/oauth/token`;
    const headers = {
        "Accept": "application/json",
        "Content-Type": "application/x-www-form-urlencoded"
    }
    const body = new URLSearchParams();
    body.append("grant_type", "client_credentials");
    body.append("client_id", this.clientId);
    body.append("client_secret", this.clientSecret);
    body.append("scope", `customer=${this.costumerEmail}`);
    console.log(route, headers, body);
    return axios.post(route, body, {headers: headers})
        .then(res => {
            return Promise.resolve(res);
        })
        .catch(err => {
            return Promise.reject(err.response);
        });
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

POST请求适用于Postman,但不适用于axios或.fetch()

Axios获得授权不适用于Vue,但适用于POSTMAN(适用于vue的Post方法)

API适用于Postman,但不适用于浏览器

API 调用适用于 postman 但不适用于 filemaker

代码适用于Postman,但不适用于localhost Ajax

POST Request适用于Postman,但不适用于Guzzle

POST请求适用于Postman,但不适用于Python

PHP - GET 请求适用于 Postman,但不适用于 cUrl

POST 调用适用于 Postman,但不适用于 Flutter

Axios 标头适用于 Django GET 请求,但不适用于 PUT 请求

Node.js API-适用于Postman,但不适用于Angular.js

AWS Cognito身份验证适用于Postman,但不适用于Angular Web应用程序

端点或路由不准确?(axios 调用适用于代码,但不适用于邮递员)

API适用于POSTMAN,不适用于Retrofit Android

使用 Axios Node.js 请求 oAuth2 令牌 - 使用“请求”但不适用于 Axios

Django中的Jsonresponse可在浏览器中工作,但不适用于PostMan或Angular

Firebase 云功能可与 Postman 一起使用,但不适用于应用

SOAP 请求适用于 Postman,而不适用于 PHP

Set-Cookie 不适用于 Opera/Firefox,但适用于 Postman。(表示)

调用 auth0.com/oauth/token:适用于 Postman,而不适用于 Angular

POST请求不适用于POSTMAN上的JSON

Rest API post 请求不适用于 Mac 和 Ubuntu 的 Postman,但仅适用于 Windows

Axios修补程序请求不适用于Laravel

适用于 PHP 但不适用于脚本

React Axios和Fetch POST方法不适用于C#WebAPI令牌请求

Photon PUN2 Unity3D - 发送 RPC 可在独立 (PC) 上运行,但不适用于 Android

缩进指南插件不再适用于 Eclipse Photon 2018?

Postman JSON 不适用于 ASP.NET Core Web API

Postgres 查询适用于 SQL 客户端,但不适用于 Hibernate