Axios: 400 Antwort auf bitbucket.org

Erstellt am 3. Nov. 2017  ·  3Kommentare  ·  Quelle: axios/axios

node: 8.9.0
axios: 0.16.2
  • Wenn ich axios auf nodejs verwende, das die Authentifizierungsheader festlegt, gibt mir bitbucket einen 400-Fehler.
  • Die Verwendung derselben Anfrage mit CURL funktioniert.

Sie haben jedoch die äußerst massive und browserzentrierte Diskussion über CORS-Post an Remote-Server gesperrt:

  1. bitbucket erlaubt Anrufe von anderen Domains
  2. Ich benutze keinen Browser um die Anrufe zu tätigen
    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"}

Hilfreichster Kommentar

Ich habe es behoben, indem ich für meine serverseitigen API-Anforderungen anstelle von Axios zu require('request'); gewechselt habe.

  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;
      });

Alle 3 Kommentare

Ich habe es behoben, indem ich für meine serverseitigen API-Anforderungen anstelle von Axios zu require('request'); gewechselt habe.

  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;
      });

Da es sich um ein Axios-Problem handelt, habe ich es mit Axios so gehandhabt:

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);
  }
};

Da es sich um ein Axios-Problem handelt, habe ich es mit Axios so gehandhabt:

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);
  }
};

Dankeschön!

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen