node: 8.9.0
axios: 0.16.2
You've locked the utterly massive and browser-centric discussion on CORS post to remote servers, however:
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"}
fixed it by switching to require('request');
for my server side api requests instead of axios.
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;
});
As it's an axios issue, here's how I handled it using 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);
}
};
As it's an axios issue, here's how I handled it using 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); } };
Thank you!
Most helpful comment
fixed it by switching to
require('request');
for my server side api requests instead of axios.