angularjs ignored added headers - angularjs

i am tyring to get google api token by sendding auth code to google following this guide.
this is my code:
$http({
method: 'POST',
url: 'https://accounts.google.com/o/oauth2/token',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
params : {
code : '4/heMv6ozWwCxS5RyTzCgThAgxvRyk.oske-bNEGOUTOl05ti8ZT3YnwwH8iQI',
client_id : GoogleAppInfo.clientId,
redirect_uri : GoogleAppInfo.redirect_uri,
client_secret : GoogleAppInfo.client_secret,
grant_type : 'authorization_code'
}
}).
success(function(data, status, headers, config) {
alert('secsses');
}).
error(function(data, status, headers, config) {
alert('error');
});
the reuqest sended but the header is not set!
has anyone know what is the problem?

From: Angular, content type is not being sent with $http
"You need to include a body with the request. Angular removes the content-type header otherwise.
Add data: '' to the argument to $http."

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.

how to remove Access-Control-Allow-Origin angular application while Getting Data?

After payment gateway success it redirect to http://localhost/august1stZustshop/#/home.In response headers i got information data,status accurately.
But Using $https it makes XMLHttpRequest cannot load https://secure.payu.in/_payment.
No 'Access- Control-Allow-Origin' header is present on the requested resource.Origin
'http://localhost' is therefore not allowed access.
$http({
url: "https://secure.payu.in/_payment",
method: "GET",
}).success(function(data, status, headers, config) {
console.log(data)
console.log(status)
}).error(function(data, status, headers, config) {
$scope.status = status;
console.log(status)
console.log(data)
})
Please tell me how to get the response headers data or how to remove cors error.

"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.

Paypal API with angular and cURL

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

Resources