This is driving me nuts!
If the method is POST, why Azure is returning an "AADSTS90056: This endpoint only accepts POST, OPTIONS requests. Received a GET request" error?
Code:
$url='http://login.microsoftonline.com/common/oauth2/v2.0/token';
$data = array('code'=>$code,'resource'=>$resource,'redirect_uri' => $redirect_uri, 'client_id' => $client_ID, 'scope' => $scope, 'grant_type' => $grant_type, 'client_secret' => $client_secret);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
Azure OAuth 2.0 endpoint is only accessible via HTTPS and that HTTPS is enforced via a 302 redirect if we make a request to it with plain HTTP. This will cause the HTTP verb to change to GET. So, you'll need to use HTTPS instead of HTTP to make it work.
Related
I am trying to execute the following Microsoft Graph request:
$url = 'https://graph.microsoft.com/v1.0/me/calendarview?startdatetime=2020-04-17T12:13:36.933Z&enddatetime=2020-04-24T12:13:36.933Z';
$data = array('grant_type' => 'authorization_code', 'client_id' => '<myclientid>', 'client_secret' => '<myclientsecret>', 'redirect_uri' => 'http://localhost/myapp/request.php', 'code' => '<myauthorisationcode>');
$options = array(
'http' => array(
'header' => "Authorization: Bearer <myaccesstoken>",
'header' => "Host: login.microsoftonline.com",
'header' => "Content-type: application/json",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
Actually I should get information about my calendar now. But I receive the following warning:
Warning: file_get_contents(https://graph.microsoft.com/v1.0/me/calendarview?startdatetime=2020-04-17T12:13:36.933Z&enddatetime=2020-04-24T12:13:36.933Z): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in C:\xampp\htdocs\myapp\request.php on line 16
bool(false)
In Azure I have added the following 3 API permissions: Calendars.Read, User.Read, User.Read.All.
my API Authorizations
Does anyone know why my request has not been accepted?
Thanks for your help!
I'm trying to connect to an OAuth2 Provider using cURL in PHP 5. I established the initial authorization, and got that working fine.
However, I'm getting stuck with getting it to send me a token. I keep hitting the "unsupported_grant_type" error. This is my code so far:
$token_url = 'https://discordapp.com/api/oauth2/token';
if (!$code)
{
echo 'Login with Discord';
} else {
// cURL Request Goes Here.
$chB = curl_init();
$post_opts = array(
'client_id' => '[id_goes_here]',
'client_secret' => '[secret_goes_here',
'code' => $code,
'redirect_uri' => '[valid_redirect_uri_goes_here]',
'grant_type' => 'authorization_code',
);
$c_opts = array(
CURLOPT_URL => $token_url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
'Content-type: application/x-www-form-urlencoded',
),
CURLOPT_POSTFIELDS => $post_opts,
);
curl_setopt_array($chB, $c_opts);
$run_now = curl_exec($chB);
$code = curl_getinfo($chB, CURLINFO_HTTP_CODE);
curl_close($chB);
print($code);
var_dump($run_now);
}
As you can see, I'm sending the data post to the authorization_code grant type. $code is the returned code form the authorize endpoint. I also have my form data set as x-www-form-urlencoded.
Can someone please point me into the direction of what I'm doing wrong here?
I've searched for a few hours now on Google, SO, etc... I'm not seeing any fresh advice that's getting me anywhere else other than where I already am.
Currently working on a tool using the Runkeeper api but running (haha) into some issues regarding the post request I need to make in orde to fully authorize the thing.
I currently use this code:
$grant_type ='authorization_code';
$code = $codeR;
$client_id = $this->client_id;
$client_secret = $this->client_secret;
$redirect_uri = $this->req_url;
$url = 'http://api.runkeeper.com';
$data = array('grant_type' => $grant_type, 'code' => $code, 'client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_url' => $redirect_uri);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url,false,'',$options);
return $result;
The codeR parameter is succesfully recieved after the user has been redirected to grand permission.
Anyone who can help?
Thanks!
I had a problem with no documented solution that I could find. Now that I found it, I'm posting it here in the event someone runs into the same issue.
I followed the steps to authenticate with LinkedIn and get an access token, I was able to retrieve my profile information and groups that I belong to without any issue.
Next, I wanted to make a post to a group using the API.
The LinkedIn API docs show the use of file_get_contents, but it was not working for me. The access token was correct, but I was receiving a 401 response. Refer to https://developer.linkedin.com/documents/code-samples. Because I added ignore_errors=1, the group post was made, but still returning a 401.
As reference, this was the piece of code that I had to change to resolve the 401:
$context = stream_context_create(
array('http' =>
array('method' =>"POST",
'header'=> "Content-Type:application/json\r\n",
'content' => $body,
'ignore_errors' => '1'
)
)
);
$res = file_get_contents($url, false, $context);
Solution Overview
Using the LinkedIn API to post to a group, the steps are:
Set up the URL:
$params = array('oauth2_access_token' => YOUR_ACCESS_TOKEN);
$url = 'https://api.linkedin.com/v1/groups/{group_id}/posts?' . http_build_query($params);
Set the body for the POST
$bodyArray = array(
'title' => $title,
'summary' => $userMessage,
'content' => array(
'title' => '$title2',
'submitted-image-url' => $pictureUrl,
'submitted-url' => $redirectUrl,
'description' => $userMessage
)
);
$body = json_encode($bodyArray);
Use CURL instead of get_file_contents This is what I needed to change to get it working.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"),
CURLOPT_POSTFIELDS => $body,
));
// here we execute the code and check for response code
curl_exec($curl);
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($http_status == "201"){
echo date('g:i') . ' Posted to LinkedIn group <br>';
}else{
echo date('g:i') . '<b>LinkedIn error: ' . $http_status . '</b><br>';
}
Can anyone give me any explanation for why this authorization function for a private bitbucket repository is working on my local machine (running PHP Version 5.3.17) but is not authorizing on my remote server (running PHP Version 5.3.20)
I'm not getting an error per se -- i'm just getting a "forbidden" response from bitbucket. But everything works great running from my local server.
function bitBucketConnect($url){
global $bitPassword;
global $bitUsername;
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$bitUsername:$bitPassword")
)
));
// Make the request
return file_get_contents($url, false, $context);
}
Your proxy will respond that authentication is required. You may scratch your head and think "but I'm providing authentication!"
The issue is that the 'header' value is only applicable to http connections. So to authenticate on a proxy, you first have to pull a file from HTTP, before the context is valid for using on FTP.
<?php
$opts = array('ftp' => array(
'proxy' => 'tcp://vbinprst10:8080',
'request_fulluri'=>true,
'header' => array(
"Proxy-Authorization: Basic $auth"
)
),
'http' => array(
'proxy' => 'tcp://vbinprst10:8080',
'request_fulluri'=>true,
'header' => array(
"Proxy-Authorization: Basic $auth"
)
)
);
$context = stream_context_create($opts);
$s = file_get_contents("http://www.example.com",false,$context);
$s = file_get_contents("ftp://anonymous:anonymous#ftp.example.org",false,$context);
?>