Azure Graph API : Authentication_MissingOrMalformed - azure

I am getting the Authentication_MissingOrMalformed, getting the token properly and assigned in the header.
// Curl call for calling graph API
curl_setopt_array($curl, array(
CURLOPT_URL => "https://graph.windows.net/xxxx/users/giri#xxxx.com/memberOf?api-version=1.6",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_PROXY => "xxxxx:8080",
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "api-version=1.0",
// Adding access toker here
CURLOPT_HTTPHEADER => array(
"authorization: Bearer ".$result->access_token,
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
),

The error information Authentication_MissingOrMalformed it means that the access resource is not match the aud of accesstoken.
As Juunas mentioned that you need to change the resource to https://graph.windows.net during get the accesstoken.
According to GetUsersMemberships API, we could know that content-type should be application/json and the http method is GET.
Content-Type: application/json
So you also need to change content-type from application/x-www-form-urlencoded to application/json.

Related

Fetch Order Details Of A Particular Sellers From Flipkart To Our Website

Hi Developer,
I am working on a website where I want to fetch all my client Flipkart seller's order details to my client website from where they can manage their order, what will be the possible way to integrate it, Will Flipkart allow me to see my Client order details on the website
Thanks!
using this you will get orders
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.flipkart.net/sellers/v2/orders/search/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"filter": {
"orderDate": {
"fromDate": "2022-02-01T00:00:01.19800Z",
"toDate": "2022-02-03T20:43:30.19800Z"
}
}
}',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer $token',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

PUT call to RESTapi giving error "SyntaxError: Unexpected token B in JSON at position 0"

I'm working with React, Typescript and a self-made RESTapi.
I'm trying to make a PUT request to my api from my client in React, but i keep on getting the error "SyntaxError: Unexpected token B in JSON at position 0". The PUT call to the api contains no body, nor should it due to the way I created the request in my API. I have confirmed that the url in fetch is the correct one, and that the API call works perfectly fine when testing it directly in my API. Therefore the problem seems to be something with the way i make the PUT call in the client
Below is my PUT call from my client.
Thanks a lot in advance!
function putProductInBasket(id: number){
fetch("http://localhost:3005/velocishop/customers/" + localStorage.uuid + "/baskets/" + localStorage.uuid + "/products/" + id, {
method: "PUT",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
})
.then(response => {
if (response.ok)
return Promise.resolve ({ status:response.statusText})
else
return Promise.reject ({ status:response.statusText})
}
)
.catch(err => console.log(err))
}
You need to first convert the response from fetch into json by using res.json()
Something like this
fetch('/data.json')
.then(response => response.json()) // Add this .then block before your first .then block
.then(data => {
console.log(data)
})
.catch(err => ...)
If you set the Content-Type: application/json body, not sending a body is invalid because the expectation is that you actually send JSON.
Removing this header might fix your problem.

Polar accesslink API POST users not working

I'm currently trying to implement Polar accesslink API on an app, but when trying to POST a new user I still get this error:
url: 'https://www.polaraccesslink.com/v3/users/',
status: 400,
statusText: 'Bad Request',
headers: Headers { [Symbol(map)]: [Object: null prototype] },
counter: 0
I already have the authorization token, which I know expires every 10min, and I'm using the service through a function that takes the token and the userID as parameters. This is my code:
postUser(memberId: string, token: string) {
const inputBody = { 'member-id': memberId };
const headers = {
'Content-Type': 'application/xml', 'Accept': 'application/json', 'Authorization': 'Bearer ' + token
};
const options = {
method: "POST",
headers: headers,
body: JSON.stringify(inputBody)
}
return new Promise<object>((resolve, reject) => {
fetch('https://www.polaraccesslink.com/v3/users', options)
.then(function(res: any) {
console.log(res)
return res.json();
}).then(function(body: any) {
console.log(body);
});
})
}
I'm implementing it the same way as it is specified in https://www.polar.com/accesslink-api/?javascript--nodejs#users but really don't know what might I be doing wrong. Thanks for helping me!.
I don't have experience with this specific API but i can see you send in the header Content-Type the value application/xml but the request body is JSON formatted.
Try send application/json in that header.
The Content-Type header is used in HTTP to specify the body mime type.
more info in: Content-Type HTTP Header - MDN
I also see this is the exact code in the sample but notice they have 2 sample requests and 2 sample results, one in XML and one in JSON each.
Any ideea why this POST gives 'invalid_Request'?
curl --location --request POST 'https://polarremote.com/v2/oauth2/token?grant_type=authorization_code&code=1f9edc0c5e60a0bab4fd3f1f00571a58' --header 'Authorization: Basic ... DA4OS05ZDc2LTJlNTQwZjFkZTc5ZA==' --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json'

Sending post request from axios with "Content-Type" : "application/x-www-form-urlencoded" gives an 401 Unauthorized response

I am sending a POST request to a server to fetch a token through axios with a Content-Type header of x-www-form-urlencoded. I tried the same with postman and it works fine. I'm sending a key value pair of grant_type and client_credentials in the request body.
This is my axios request:
axios.post(`${baseURI}/protocol/openid-connect/token`, data, {
headers : {
"Authorization" : "Basic " + token,
"Content-Type" : "application/x-www-form-urlencoded"
},
withCredentials: true
}).then(response => {
AUTH_TOKEN = response.data.access_token;
console.log(response.data);
}).catch(error => {
console.log(error.response);
})
The data object consists of the client_credentials.The same credentials gives a successful response in postman.
I had this exact same problem until I finally figured out that Axios needed the data object to be reformatted as a query string.
So make yourself a function like this:
function getQueryString(data = {}) {
return Object.entries(data)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&');
}
Pretty simple, just URI encoding all the parts of your object and joining them with &.
Then modify your code like this:
axios.post(`${baseURI}/protocol/openid-connect/token`,data, {
headers : {
"Authorization" : "Basic " + token,
"Content-Type" : "application/x-www-form-urlencoded"
},
withCredentials: true,
transformRequest: getQueryString
})
.then(/*...*/);
You can read about the different options, including transformRequest, for the request config here: https://github.com/axios/axios#request-config
(I'm still annoyed that this was necessary and not just handled by Axios but oh well.)
While #binary lobster solution is pretty concise, one might consider using qs library like this:
import qs from 'qs'
axios.post(`${baseURI}/protocol/openid-connect/token`,
qs.stringify(data), {
headers : {
"Authorization" : "Basic " + token,
"Content-Type" : "application/x-www-form-urlencoded"
}
})
.then(/*...*/);
See more information here

Microsoft Azure Media Service. Unable to get token (app-only) by using REST API

I'm building simple Angular2 application where a given user can upload videos via Microsoft Azure Media Service REST API.
I'm trying to get a token by using this request:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=client_credentials&resource=https://rest.media.azure.net/&client_id=<application id>&client_secret=<password you selected for authentication>" https://login.microsoftonline.com/<Azure AD Tenant ID>/oauth2/token?api-version=2.11
But it will not work via AJAX (that's because CORS), so is there any other simple way to get an app-only token?
I've created php script which returns token, I think it's fastest solution.
Here is code, maybe will be useful for someone:
<?php
define(APP_CLIENT_ID, '<client_id>');
define(CLIENT_SECRET, '<client_secret>');
define(AD_TENANT_ID, '<tenant_id>');
define(RESOURCE_URL, 'https://rest.media.azure.net');
function curlTokenRequest() {
$url = "https://login.microsoftonline.com/".AD_TENANT_ID."/oauth2/token";
$request = "grant_type=client_credentials&resource=".RESOURCE_URL."&client_id=".APP_CLIENT_ID."&client_secret=".CLIENT_SECRET;
$ch = curl_init($url);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => false, // follow redirects
// CURLOPT_ENCODING => "utf-8", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 200, // timeout on connect
CURLOPT_TIMEOUT => 200, // timeout on response
CURLOPT_POST => 1, // i am sending post data
CURLOPT_POSTFIELDS => $request, // this are my post vars
CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
CURLOPT_SSL_VERIFYPEER => false, //
CURLOPT_VERBOSE => 1,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded"
)
);
curl_setopt_array($ch,$options);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
//echo $curl_errno;
//echo $curl_error;
curl_close($ch);
http_response_code($httpcode);
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
return $data;
}

Resources