Paypal API with angularjs - angularjs

I'm making a PayPal API call in the context of an e-Commerce solution I'm developing in Angular-JS. I was able to get an auth_token back from PayPal using the following code.
my code :
var basicAuthString = btoa('AUAlrIhHNoiQlmaW.............:EJk......');
$http({
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + basicAuthString,
},
data: { 'grant_type': 'client_credentials' }
}).success(function (data, status, headers, config) {
console.log(data)
}).error(function (error) {
console.log("erreur "+error);
});
When sending the data , I receiving an error
error:{"error":"unsupported_grant_type","error_description":"Grant Type is NULL"}

You should send the grant type like this:
data: 'grant_type=client_credentials'

Related

post data to the server using angularjs

This works for me
// default post header
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
// send login data
$http({
method: 'POST',
url: 'abc.php',
data: $.param({
email: "abc#gmail.com",
password: "password"
}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function (data, status, headers, config) {
// handle success things
}).catch(function (data, status, headers, config) {
// handle error things
});
This does not
$http.post('abc.php', {email: "abc#gmail.com",
password: "password"})
.then(function(res){
$scope.response = res.data;
})
Hi could you please elobrate why first implementation is working second doesn't. I'm very confused with short cut and long cut angular method
Thanks in advance
Fundamentally, the problem lies with your server language of choice being unable to understand AngularJS’s transmission natively.
$http.post is transmitting your data as Content-Type: application/json
Change the header to URL Encoded as below :-
$http.post("abc.php", requestData, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
transformRequest: transform
}).success(function(responseData) {
//do stuff with response
});
The second snippet should like this
$http.post('abc.php', {email: "abc#gmail.com",
password: "password"})
}).then(function(res){
$scope.response = res.data;
});

angular ionic $http sends null data to rest API

I am calling a rest API using
method is "POST"
requestData is
{
'username': user.userName,
'password': user.password,
'deviceID': deviceid,
'latlng': null,
'pincode': null
}
var req = {
method: method,
url: 'http://url/' + Api,
headers: {
'Token': 'Basic bUY5VkV0eHBOK3JUYUF1TGJjM1FHRGh4N1hYejhYSEw=',
'Content-Type': 'application/json'
},
data: {
'contentType': 'application/json',
'content': requestData
},
}
console.log(req);
$http(req).then(successCallback, errorCallback);
My rest APA is
#RequestMapping(value = "/authenticateUser", method = RequestMethod.POST, consumes = "application/json")
public AuthenticateUserBean authenticateUser(#RequestBody final AuthenticateUserBean authenticateUserBean) {
I am receiving null values for all the data such as username, password.
It is an ionic hybrid app with angular js.
I am calling it using chrome.
Cors is already disabled.
Your request format has few issues:
No need of contentType in data
You can directly send your requestData as data
Try with below code:
var req = {
method: method,
url: 'http://url/' + Api,
dataType: 'json',
headers: {
'Token': 'Basic bUY5VkV0eHBOK3JUYUF1TGJjM1FHRGh4N1hYejhYSEw=',
'Content-Type': 'application/json'
},
data: requestData,
}
console.log(req);
$http(req).then(successCallback, errorCallback);

null with receipt data in angular $http

This is my code:
$scope.doLogin = function() {
$http({
method: "POST",
url: "http://localhost:8000/api/v1/user/user_signin",
data: { username: $scope.user.username, password: $scope.user.password },
headers: { 'Content-Type': 'application/json' },
responseType:'json'
}).success(function (data) {
console.log('data success');
console.log(JSON.stringify(data));
})
.error(function(data, status, headers,config){
console.log('data error');
});
};
Consol says:
data success
null
What I do wrong?
P.S.: API request is correct - I checked it in postman.
You set responseType to 'json' make sure that your server side is also sending the response compatible to your format 'json'.
Or simply remove the response type check if it works

How to get authenticated Json object in angularjs

I want to get an object by using get request in angularJs. Server asks for authentication details: how to pass authentication details?
$http({
method: 'GET',
url: 'http://192.168.1.133:8080/xyz-api/api/customer/token/xyzname',
headers: {
'Content-Type': 'application/JSON'
}
}).success(function (data) {
$scope.customerDetails = data;
$scope.CUSTOMER_ID = $scope.customerDetails.id;
$scope.companyName = $scope.customerDetails.name;
}).error(function () {
alert('error');
});

ASP.NET MVC Web API Primitive Post not binding Angular

I have the following Action
[HttpPost]
[Route("api/client/log")]
public void SetClientLog([FromBody]string error)
{
ClientLogger.LogError(error);
}
And in angular i'm trying to post the following where log is just a string
$http({
method: 'POST',
url: "api/client/log",
data: log,
headers: {'Content-Type': 'application/json'}
});
Make sure you prefix your value with "=" and set the correct Content-Type:
$http.post('api/Companies/Search', "="+$scope.query, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }
})
.success(function (data, status) {
$scope.result = data;
})
.error(function (data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});

Resources