4

I am trying to sign in with google plus Api in my web app.

I searched on it from https://developers.google.com/oauthplayground/https://developers.google.com/oauthplayground/ and I applied them.

When I request to https://www.googleapis.com/oauth2/v3/token , it returns

{ "error": "internal_failure", "error_description": "Unsupported content with type: multipart/form-data; boundary=----------------------------5dd1639f2986" }

I am putting my code snippets which are doing the request (as client id and secret as star) I dont understand it, maybe you can help.

if(isset($_GET['code'])) {
// try to get an access token
$code = $_GET['code'];
$url = 'https://www.googleapis.com/oauth2/v3/token';
$params = array(
    "code" => $code,
    "client_id" => "************************",
    "client_secret" => "************************",
    "redirect_uri" => "http://localhost/googleplus/oauth2callback.php",
    "grant_type" => "authorization_code"
);



$json=CallAPI('POST',$url,$params);

and my CALLAPI function

function CallAPI($method, $url, $data = false)
{

$curl = curl_init();

switch ($method)
{
    case "POST":
        curl_setopt($curl, CURLOPT_POST, 1);

        if ($data)
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        break;
    case "PUT":
        curl_setopt($curl, CURLOPT_PUT, 1);
        break;
    default:
        if ($data){
            $url = sprintf("%s?%s", $url, http_build_query($data));
            echo $url;
        }
}

// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($curl);

curl_close($curl);



return $result;
}
DaImTo
  • 72,534
  • 21
  • 122
  • 346
OzgurGundogan
  • 73
  • 1
  • 6

4 Answers4

1

You request is correct but missing something the content type is not multipart/form-data should be application/x-www-form-urlencoded instead

Ahmad
  • 169
  • 2
  • 6
0

I had very similar problem using OAuth2.0 to access Gmail from iOS app. Indeed Ahmad's suggestion is correct I'll expand it:

When you POST using HTTP there is a header field 'Content-Type:'. Looks like in your case it had default value of 'multipart/form-data' which is for binary data. For Google API OAuth2.0 make sure you set it to 'application/x-www-form-urlencoded'.

You can see this value in the example given here: https://developers.google.com/identity/protocols/OAuth2InstalledApp#handlingtheresponse

You can read more about those two values for 'Content-Type' here: application/x-www-form-urlencoded or multipart/form-data?

In my iOS framework I had to make a call to customize this HTTP header. Unfortunately I'm not familiar with PHP so hope that helps.

Community
  • 1
  • 1
gheni4
  • 211
  • 3
  • 13
0

I had a very similar problem. I was finally able to resolve it by adding some elements to the HTTP header ("Content-Type" and "Content-Length") and manually constructing the url (so I can use 0 for "Content-Length").

$id_token = $_POST['id_token'];
$url = 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token='.$id_token;
$options = [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => array( "Content-Length: 0", "Content-Type: application/x-www-form-urlencoded"),
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_SSL_VERIFYPEER => false,
];

$curlObj = curl_init();
curl_setopt_array($curlObj, $options);
$returnData = curl_exec($curlObj);
if (curl_errno($curlObj)) {
    //error message
    $returnData = curl_error($curlObj);
}

echo $returnData;
-1
      if(isset($_REQUEST['code'])){
$ch =curl_init();
$auth_url = "https://www.googleapis.com/oauth2/v4/token";
$post = array(
        'code' =>$_REQUEST['code'],
        'client_id' => '**************',
        'client_secret' => '************',
        'redirect_uri' => 'http://localhost/prakash/redirect.php',
        'grant_type' => 'authorization_code'
        );
$postText = http_build_query($post);
$options = array(
    CURLOPT_URL =>$auth_url,
    CURLOPT_POST => true,
     CURLOPT_SSL_VERIFYPEER =>false,
    CURLOPT_RETURNTRANSFER =>true,
    CURLOPT_POSTFIELDS => $postText
);
//print_r($ch);
curl_setopt_array($ch, $options);
$returnData = curl_exec($ch);
print_r($returnData);
curl_close($ch);
    exit;
}else{
    print_r($_REQUEST);
    exit;
}
UI Developer
  • 356
  • 3
  • 9