Jump to content

End Point


hackalive

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.

Link to comment
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: /

Link to comment
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.

Link to comment
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.

Link to comment
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
Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.