Reputation: 3113
I have a CURL request which is working properly:
curl -v -X POST https://auth.domain.com/v1/oauth/tokens -u test:test -d "grant_type=authorization_code" -d "code=d9a473a4-e417-4dd7-9151-83e9c1cb9ca6" -d "redirect_uri=app://authorize"
I tryed to implement it in my React Native app but I always get 400 error. Firstly I used axios:
var url = `https://auth.domain.com/v1/oauth/tokens`
axios.post(url, {
"grant_type": 'authorization_code',
"code": code,
"redirect_uri": 'app://authorize',
},{
auth: {
username: 'test',
password: 'test'
}
}).then(response => {
console.log(response);
}).catch(function(error) {
console.log('There has been a problem with your fetch operation: ' + error.message);
throw error
});
But I got 400 error:
Possible Unhandled Promise Rejection (id: 0):
Request failed with status code 400
Error: Request failed with status code 400
I tryed it with fetch either:
fetch(url, {
method: 'post',
headers: {
'Authorization': 'Basic '+btoa('test:test'),
},
body: JSON.stringify({
"grant_type": 'authorization_code',
"code": code,
"redirect_uri": 'app://authorize',
})
}).then(response => {
console.log('Request core...');
console.log(response);
})
I got 400 error with the same empty body. For the CURL request I got 200 OK and the response from the server. What am I doing wrong in the JS side?
Upvotes: 0
Views: 2020
Reputation: 1013
This code Work for me.
.catch((error) => console.log( error.response.request._response ) );
Upvotes: 1
Reputation: 3113
As Yury Tarabanko mentoined the problem was "You are sending urlencoded data using curl. Other examples send json. "
Solution:
var url = `https://auth.domain.com/v1/oauth/tokens`
axios.post(url,
querystring.stringify({
"grant_type": 'authorization_code',
"code": code,
"redirect_uri": 'app://authorize',
}),{
auth: {
username: 'test',
password: 'test'
},
headers: {
'Content-type': 'application/x-www-form-urlencoded'
}
}).then(response => {
console.log('Request core...');
console.log(response);
}).catch(function(error) {
console.log('There has been a problem with your fetch operation: ' + error.message);
console.log(error);
throw error;
});
Upvotes: 0