KemChat
KemChat

Reputation: 151

Google API OAuth 2.0 CURL returning “Required parameter is missing: response_type”

I following this topic. My purpose I want to login to google with php and curl for approve access application for get authorization_code after login and approve.

I want to create script for add new post to blogger.

Here my code:

$USERNAME = '[email protected]';
$PASSWORD = 'mypassword';

$url = "https://accounts.google.com/o/oauth2/auth";
$params = array(
    "response_type" => "code",
    "client_id" => "myclient_id",
    "redirect_uri" => "redirect_uri from setting",
    "scope" => "https://www.googleapis.com/auth/blogger"); // Request Blogger authentication 

$request_to = http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_to);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie/blogger.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie/blogger.txt');  
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);
// echo $data;
$formFields = getFormFields($data);
$formFields['Email']  = $USERNAME;
$formFields['Passwd'] = $PASSWORD;
unset($formFields['PersistentCookie']);
$post_string = '';
foreach($formFields as $key => $value) {
    $post_string .= $key . '=' . urlencode($value) . '&';
}
$post_string = substr($post_string, 0, -1);
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($ch);
echo $result;

function getFormFields($data){
    if (preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) {
        $inputs = getInputs($matches[1]);

        return $inputs;
    } else {
        die('didnt find login form');
    }
}

function getInputs($form)
{
    $inputs = array();

    $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);

    if ($elements > 0) {
        for($i = 0; $i < $elements; $i++) {
            $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);

            if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
                $name  = $name[1];
                $value = '';

                if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
                    $value = $value[1];
                }

                $inputs[$name] = $value;
            }
        }
    }

    return $inputs;
}

From this code I have error message.

Error: invalid_request
Required parameter is missing: response_type

When I see request url. It's have parameter "response_type".

How do I do?

Excuse me. I'm not good english.

Upvotes: 0

Views: 1575

Answers (1)

Misunderstood
Misunderstood

Reputation: 5665

You are putting the params in the wrong place.

curl_setopt($ch, CURLOPT_URL,$request_to);

should be:

curl_setopt($ch, CURLOPT_URL,$url);

It seems you accidentally deleted some options here:

curl_setopt($ch, CURLOPT_URL, 
  $request_to);
$data = curl_exec($ch);

UPDATE

I have not gotten to the second request, the first looks a mess.

It looks like you copied and pasted a mishmash of nonsensical code.

The params make no sense.

$params = array(
    "response_type" => "code",
    "client_id" => "myclient_id",
    "redirect_uri" => "redirect_uri from setting",
    "scope" => "https://www.googleapis.com/auth/blogger");

This is the best I could do to clean up the curl options for the first request:

$request_to = http_build_query($params);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_to);
curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie/blogger.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie/blogger.txt');  
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);

Upvotes: 1

Related Questions