Axios: 400 响应 bitbucket.org

创建于 2017-11-03  ·  3评论  ·  资料来源: axios/axios

node: 8.9.0
axios: 0.16.2
  • 在 nodejs 上使用 axios 设置身份验证标头,bitbucket 给了我 400 错误。
  • 对 CURL 使用相同的请求有效。

您已经锁定了关于 CORS 发布到远程服务器的完全大规模且以浏览器为中心的讨论,但是:

  1. bitbucket 允许来自其他域的调用
  2. 我没有使用浏览器拨打电话
    axios
      .post(`https://bitbucket.org/site/oauth2/access_token`, {
        auth: {
          username: process.env.BITBUCKET_CLIENT_ID,
          password: process.env.BITBUCKET_CLIENT_SECRET
        },
        { grant_type: 'access_token' }
      })
      .then(response => {

        log('oauth.auth.response', response.data);
        const {
          access_token,
          refresh_token,
          token_type,
          scopes,
          expires_in
        } = response.data;
      });
...
$ curl -u $BITBUCKET_CLIENT_ID:$BITBUCKET_CLIENT_SECRET \
 https://bitbucket.org/site/oauth2/access_token \
-d grant_type=client_credentials

{"access_token": "magically-correct-response=", "scopes": "webhook repository team accou
nt", "expires_in": 3600, "refresh_token": "maigcally-correct-resfresh-token", "token_type": "bearer"}

最有用的评论

通过为我的服务器端 api 请求而不是 axios 切换到require('request');来修复它。

  require('request')
      .post(`https://bitbucket.org/site/oauth2/access_token`, {
        auth: {
          username: process.env.BITBUCKET_CLIENT_ID,
          password: process.env.BITBUCKET_CLIENT_SECRET
        },
        form: { grant_type: 'access_token' }
      }, (err, response, body) => {
        const data = JSON.parse(body);
        log('oauth.auth.response', data);
        const {
          access_token,
          refresh_token,
          token_type,
          scopes,
          expires_in
        } = data;
      });

所有3条评论

通过为我的服务器端 api 请求而不是 axios 切换到require('request');来修复它。

  require('request')
      .post(`https://bitbucket.org/site/oauth2/access_token`, {
        auth: {
          username: process.env.BITBUCKET_CLIENT_ID,
          password: process.env.BITBUCKET_CLIENT_SECRET
        },
        form: { grant_type: 'access_token' }
      }, (err, response, body) => {
        const data = JSON.parse(body);
        log('oauth.auth.response', data);
        const {
          access_token,
          refresh_token,
          token_type,
          scopes,
          expires_in
        } = data;
      });

由于这是一个 axios 问题,以下是我使用 axios 处理它的方法:

import axios from 'axios';
import * as qs from 'querystring';

export const Callback = async (req, res): Promise<void | never> => {
  const {
    BITBUCKET_CLIENT_ID,
    BITBUCKET_CLIENT_SECRET,
    BITBUCKET_CALLBACK_URL
  } = process.env;

  const { code: AUTH_CODE } = req.query;

  try {
    const data = await axios({
      url: 'https://bitbucket.org/site/oauth2/access_token',
      headers: {
        'Cache-Control': 'no-cache',
        'content-type': `application/x-www-form-urlencoded`,
      },
      auth: {
        username: BITBUCKET_CLIENT_ID,
        password: BITBUCKET_CLIENT_SECRET,
      },
      method: 'post',
      data: qs.stringify({
        grant_type: 'authorization_code',
        code: AUTH_CODE,
        redirect_uri: BITBUCKET_CALLBACK_URL,
      }),
    });

    // Do something

  } catch (e) {
    console.trace(error);
    return res.sendStatus(400);
  }
};

由于这是一个 axios 问题,以下是我使用 axios 处理它的方法:

import axios from 'axios';
import * as qs from 'querystring';

export const Callback = async (req, res): Promise<void | never> => {
  const {
    BITBUCKET_CLIENT_ID,
    BITBUCKET_CLIENT_SECRET,
    BITBUCKET_CALLBACK_URL
  } = process.env;

  const { code: AUTH_CODE } = req.query;

  try {
    const data = await axios({
      url: 'https://bitbucket.org/site/oauth2/access_token',
      headers: {
        'Cache-Control': 'no-cache',
        'content-type': `application/x-www-form-urlencoded`,
      },
      auth: {
        username: BITBUCKET_CLIENT_ID,
        password: BITBUCKET_CLIENT_SECRET,
      },
      method: 'post',
      data: qs.stringify({
        grant_type: 'authorization_code',
        code: AUTH_CODE,
        redirect_uri: BITBUCKET_CALLBACK_URL,
      }),
    });

    // Do something

  } catch (e) {
    console.trace(error);
    return res.sendStatus(400);
  }
};

谢谢!

此页面是否有帮助?
0 / 5 - 0 等级