invalid request when using OAuth 2 authentication
I have an app written in C# to authorize access to a visitors Google data
using OAuth 2. This sends the following to get the access token and
refresh value
POST https://accounts.google.com/o/oauth2/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded; charset=utf-8
User-Agent: DotNetOpenAuth/4.1.3.12303
Host: accounts.google.com
Cache-Control: no-store,no-cache
Pragma: no-cache
Content-Length: 256
Expect: 100-continue
Connection: Keep-Alive
code=4%2Fb1uuk5vvvvvvvvvvwwwwwwwwwwxxxxxxxxxxzzzzzzzzzzi8ZT3Zfe8D8gQI&
redirect_uri=url&
grant_type=authorization_code&
client_id=myClientId&
client_secret=myClientSecret
The C# code works fine, however for various reasons I need to implement
this in php. My PHP code is sending the following;
POST https://accounts.google.com/o/oauth2/token HTTP/1.1
Host: accounts.google.com
Accept: */*
Content-Length: 737
Expect: 100-continue
Content-Type: multipart/form-data;
boundary=----------------------------36d898af3c2f
------------------------------36d898af3c2f
Content-Disposition: form-data; name="code"
4%2Fb1uuk5vvvvvvvvvvwwwwwwwwwwxxxxxxxxxxzzzzzzzzzzi8ZT3Zfe8D8gQI&
------------------------------36d898af3c2f
Content-Disposition: form-data; name="client_id"
myClientId
------------------------------36d898af3c2f
Content-Disposition: form-data; name="client_secret"
myClientSecret
------------------------------36d898af3c2f
Content-Disposition: form-data; name="redirect_uri"
uri
------------------------------36d898af3c2f
Content-Disposition: form-data; name="grant_type"
authorization_code
------------------------------36d898af3c2f--
The following is the PHP code which sent the following to Google
ini_set('display_errors', '1');
error_reporting(E_ALL);
$configuration_array = parse_ini_file("Configuration.ini",true);
$grant_code = urlencode($_GET['code']);
// code recieved from Google
$client_id = $configuration_array["Google"]["ConsumerKey"];
// Consumer Key from Application Registration
$client_secret = $configuration_array["Google"]["ConsumerSecret"];
// Consumer Secret from Application Registration
$developer_key = $configuration_array["Google"]["DeveloperKey"];
$redirect_url =
urlencode($configuration_array["Google"]["RedirectUri"]); // Redirect
Uri - defined in Application Registration
$oauth2token_uri = $configuration_array["Google"]["OAuth2Token"];
// Url to request Token
$clienttoken_post = array(
"code" => $grant_code,
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => $redirect_url,
"grant_type" => "authorization_code"
);
$curl = curl_init($oauth2token_uri);
// define curl object which will send request to Token Uri
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_PROXY, '127.0.0.1:8888');
$json_response = curl_exec($curl);
curl_close($curl);
echo $json_response;
And when I get invalid request - which is also what I am getting when
using Google's own php library - writing my own code just allows we to put
in the CURLOPT_PROXY request and so see what is being sent via Fiddler.
So how can I get php to send the data in the format above - which has been
sent from my C# code.
Thanks in advance
No comments:
Post a Comment