Jump to content

Archived

This topic is now archived and is closed to further replies.

hackalive

End Point

Recommended Posts

Hi guys,

I am using this OAuth library https://github.com/hswong3i

I am trying to parse the string to token.php

http://localhost/sandbox/token.php?grant_type=authorization_code&client_id=0123456789ab&client_secret=secrettest&code=3b186c90256fc572027a5eb7fb2e51f8&redirect_uri=/

 

Now this is not working :(

 

But if I use Poster (FF Plugin) and do this:

    URL: http://localhost/sandbox/token.php

    Content Type: application/x-www-form-urlencoded

    Parameter Body: grant_type=authorization_code&client_id=0123456789ab&client_secret=secrettest&code=3b186c90256fc572027a5eb7fb2e51f8&redirect_uri=/

It works!

 

So why will it not parse form URL?

 

Any help is greatly appreciated.

 

Cheers in advance.

Share this post


Link to post
Share on other sites

Well I'm not sure what that code does from https://github.com/hswong3i, but since are in the php forum, here's a way to do it.

 

I tend to favor parse_url() , although are other methods.

<?php
//$url = "http://localhost/sandbox/token.php?grant_type=authorization_code&client_id=0123456789ab&client_secret=secrettest&code=3b186c90256fc572027a5eb7fb2e51f8&redirect_uri=/";

$url_path = end(explode("/",parse_url($url, PHP_URL_PATH)));
echo  $url_path."<br />";

$url_query = parse_url($url, PHP_URL_QUERY);
echo  $url_query."<br />";

if(preg_match("/&/",$url_query)){
$parameters = explode("&",$url_query);
} else {
$parameters[] = $url_query;
}

foreach($parameters as $parameter){
$value = explode("=",$parameter);
echo "Request: " . $value[0] . " Value: " . $value[1] . "<br />";
}
?>

 

Results would be:

 

token.php

grant_type=authorization_code&client_id=0123456789ab&client_secret=secrettest&code=3b186c90256fc572027a5eb7fb2e51f8&redirect_uri=/

Request: grant_type Value: authorization_code

Request: client_id Value: 0123456789ab

Request: client_secret Value: secrettest

Request: code Value: 3b186c90256fc572027a5eb7fb2e51f8

Request: redirect_uri Value: /

Share this post


Link to post
Share on other sites

Sorry, this is the correct code link ...

https://github.com/hswong3i/oauth2-php

 

As you can see we are focused on token.php in the PDO folder

 

Also you code is not helpful as it is not parsing the ?grant_type=a.... to the token.php, its merely splitting the URL into segments and returning them in HTML to view.

 

An additional note, when using the FF Plugin, I press POST.

Share this post


Link to post
Share on other sites

perhaps i do :) thanks for pointing it out :D

 

It essentially needs to post that string (I have attached to the URL) to that URL and then do file contents get from that token.php file whcih that sting was submitted to, or at least thats what the FF Plugin simulates.

 

Cheers again.

Share this post


Link to post
Share on other sites

what do you get if you put this at the top of your token.php page:

echo " server method is set to ".$_SERVER['REQUEST_METHOD'];
print_r("<br>".$_GET);

Share this post


Link to post
Share on other sites

Please see attached, this is the export I get from the FF Plugin. Window on the right is what I enter and when I press POST the output is the left window.

 

I am now using a seperate file (2.php) with this code (below) to ensure it does the correct POST

$postdata = http_build_query(
	array(
			'grant_type' => 'authorization_code',
			'client_id' => '0123456789ab',
			'client_scret' => 'secrettest',
			'code' => '3b186c90256fc572027a5eb7fb2e51f8',
			'redirect_uri' => '/sandbox/'
	)
);

$opts = array('http' =>
	array(
			'method'  => 'POST',
			'header'  => 'Content-type: application/x-www-form-urlencoded',
			'content' => $postdata
	)
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://localhost/sandbox/token.php', false, $context);
echo $result;

 

But I get this error:

Warning: file_get_contents(http://localhost/sandbox/token.php) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in D:\public\sandbox\2.php on line 24

Share this post


Link to post
Share on other sites

I'm not getting this, Your server request method is set to get, but you have the following code:

...
$opts = array('http' =>
	array(
			'method'  => 'POST',
...

and there is nothing getting passed through the GET global when you move to the next page, even though there are variables in your url......weird

Share this post


Link to post
Share on other sites

@Muddy_Funster

The post saying I get GET was from before I made the modifications listed in the next post.

 

The post that mentions 2.php and all post onward are from where I have the code to set POST.

 

Make sense?

Share this post


Link to post
Share on other sites

Post doesn't get variable info from the URL, it is sent from the form directly to the POST superglobal. What way do you want to do things? using POST form variables or or using GET URL variables?

Share this post


Link to post
Share on other sites

if you send information between pages using method="POST" then you are not using the url, it is sent directly from the HTML form to the $_POST[] global variable.  If you are using information from the url to populate variables then you need to use method="GET".  $_GET[] 'gets' the variables from the url as the page is loaded.

Share this post


Link to post
Share on other sites

i just need to replicate the FF Plugin actions, so if someone can tell me how to do that (see screenshot above) that would be best :)

Share this post


Link to post
Share on other sites

Try this?

 

<?PHP

    //### Set the post query
    $postQuery = array('grant_type'=>'authorization_code',
                       'client_id'=>'0123456789ab',
                       'client_secret'=>'secrettest',
                       'code'=>'3b186c90256fc572027a5eb7fb2e51f8',
                       'redirect_uri'=>'/sandbox/');

    //### Initiate a new cURL
    $ch = curl_init();

          //### Set the options for the cURL request
          curl_setopt($ch, CURLOPT_URL, 'http://localhost/sandbox/token.php');
          curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
          curl_setopt($ch, CURLOPT_HEADER, false);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_POST, true);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $postQuery);
          curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $output = curl_exec($ch);
          curl_close($ch);

    //### Echo the output received
    echo $output;
?>

 

Regards, PaulRyan.

Share this post


Link to post
Share on other sites

@PaulRyan,

Perfect! Thanks heaps.

 

Also a thanks to all those who made suggestions and helped out.

 

Have a good one.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.