Paypal API with angular and cURL - angularjs

Currently I'm working with paypal API and flowing with this document make your first call.
When I sent a request through angular $http to paypal, I got a Status Code 415. It's seems like I missed something, does anyone can help me? :)
here is my code
$http({
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'POST',
headers: {'Content-Type': 'application/json',
'auth-token': 'EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp',
'Accept-Language': 'en_US'
},
data: { 'grant_type': 'client_credentials' }
}).success(function (data, status, headers, config) {
console.log(data)
}).error(function (data, status, headers, config) {
console.log(data)
});

Referring to the Paypal doc
Tip: If you're using Windows, we recommend you make cURL calls using a
Bash shell. If you're not using cURL calls, set the content-type to
application/x-www-form-urlencoded for this request.
Try set the content_type as application/x-www-form-urlencoded

Related

elastic search for AngularJS -- Cross-Origin Request Blocked

I have Installed elasticsearch-6.3.1 in windows & try to filter the data by using following code in angular 1,it gives CORS error
$http({
url: 'http://localhost:9200/empinfo/employeedetails/_search',
method: "POST",
data: "{ 'query': { 'query_string': { 'query': 'Sujit','fields':['name'] } } }",
headers: { 'Content-Type': 'application/json' }
}).success(function (data, status, headers, config) {
console.log(data);
}).error(function (data, status, headers, config) {
});
My Elastic search error in angular.js
Although I have added Allow CORS in elasticsearch.yml file.
Please help me.
You need to uncomment the lines in your yml file.
Simply remove '#' from the beginning of each line. Restart your Elasticsearch.

Sending data from form to POST request in Angular

I want to make a POST request using Angular. I am using ng-submit to submit the form to the controller where I access the factory to make the request using:
$http.post('/droplets', data)
I need the data from the form fields to put in the "data" variable above to send to my API but I can't work out how to do this when it is more than one field. How is this done?
Try this...
$http({
url: '/droplets',
method: "POST",
data: JSON.stringify({application:app, from:data1, to:data2}),
headers: {'Content-Type': 'application/json'}
}).success(function (data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).error(function (data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
};
Ref:https://docs.angularjs.org/api/ng/service/$http

"Must specify grant_type field" error when getting Oauth2 token

I've got a simple node.js backend build on Restify with OAuth2 authorization (restify-oauth2 plugin).
I've got a problem requesting a token. When I call POST method with my REST client, everything works correctly and I get access_token and token_type.
The problem occurs when I try to do the same thing in my frontend aplication made in Angular.
Here's my code:
var req = {
method: 'POST',
url: 'http://localhost:8080/token',
headers: headers,
data: {},
params: {grant_type: "client_credentials"}
};
$http(req).success(function(data, status, headers, config){
console.log('success');
}).error(function(data, status, headers, config){
console.log('error');
});
So as you can see, grant_type parameter is provided. But still the server responds with:
{"error":"invalid_request","error_description":"Must specify grant_type field."}
And here's devtools screenshot:
How do I need to change my request to make it work?
I finally solved it. The thing is that grant_type field must be passed inside a request body, not in the parameters.
Working request code:
var req = {
method: 'POST',
url: 'http://localhost:8080/token',
headers: headers,
data: "grant_type=client_credentials"
};
$http(req).success(function(data, status, headers, config){
console.log('success');
}).error(function(data, status, headers, config){
console.log('error');
});

Golang, cors and angularjs - header missing

I'm using rs/cors in my Go API to allow my Angularjs app to make direct requests. I've added the following code to configure CORS:
crs := cors.New(cors.Options{AllowCredentials: true})
n.Use(crs) //Negroni include
but I'm getting the No 'Access-Control-Allow-Origin' header is present on the requested resource message in the browser when I make a request.
My request looks like this:
var req = {
method: 'POST',
url: endPoint + version + method,
headers: {
'Authorization': 'Basic ' + btoa(':' + appId)
},
data: params
}
$http(req).
success(function(data, status, headers, config) {
callback(null, data);
}).
error(function(data, status, headers, config) {
callback(data);
});
How can I get round this?
Fixed it! I noticed that a few different headers were being sent with the request and I had to explicitly allow all of them.
AllowedHeaders: []string{"accept", "authorization", "content-type"}
I hope this helps someone.

Not getting response from Gmail API (Users.messages: send) -- running on mobile device via IonicFramework + AngularJS

I am able to send emails successfully using GMail's API with no issues. The receiver gets the email after a few seconds, but the app doesn't get a response from GMail (i.e., threadid, id, etc...) described here.
I'm running this on a mobile device via IonicFramework w/c uses angularJS by the way. What could be wrong?
$http({
method: 'POST',
url: 'https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=multipart',
headers: {
'Authorization':'Bearer '+ access_token,
'Content-Type': 'multipart/mixed; boundary="foo_bar_baz"'
},
data: emailStr
})
.success(function(data, status, headers, config){
console.log(data);
})
.error(function(data, status, headers, config){
console.log(data);
});
emailStr contains the base64-encoded message.
This is how the request details look like:
Request URL: https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=multipart
Request Headers
Accept:application/json, text/plain, */*
Authorization:Bearer <<token here>>
Content-Type:multipart/mixed; boundary="foo_bar_baz"
Origin:file://
User-Agent: <<User agent data>>
Query String Parameters
uploadType:multipart
Request Payload
--foo_bar_baz
Content-Type: application/json; charset="UTF-8"
{"raw":"<<encoded string>>"}
--foo_bar_baz--

Resources