2015-08-20 25 views
3

我正在嘗試在我的web應用中使用谷歌加Api來登錄。不受支持的內容類型:multipart/form-data in google plus api oauth2

我從https://developers.google.com/oauthplayground/https://developers.google.com/oauthplayground/ 我搜索它,我應用了它們。

當我請求https://www.googleapis.com/oauth2/v3/token,它返回

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

我把我的代碼片段,其正在做的請求(如客戶ID和祕密明星) 我不明白,也許你可以提供幫助。

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); 

和我CALLAPI功能

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; 
} 

回答

-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; 
} 
0

您的要求是正確的,但缺少的東西 內容類型不是multipart/form-data應該application/x-www-form-urlencoded代替

0

我不得不使用的OAuth2非常類似的問題.0從iOS應用程序訪問Gmail。事實上,艾哈邁德的建議是正確的我會擴大它:

當你使用HTTP POST時,有一個頭字段'Content-Type:'。看起來像你的情況它有默認值'multipart/form-data'是二進制數據。對於Google API OAuth2.0,請確保將其設置爲'application/x-www-form-urlencoded'

你可以看到這裏給出的示例中,這值: https://developers.google.com/identity/protocols/OAuth2InstalledApp#handlingtheresponse

你可以閱讀更多關於這兩個值「的Content-Type」的位置: application/x-www-form-urlencoded or multipart/form-data?

在我的iOS框架,我不得不打個電話來定製這個HTTP頭。不幸的是,我不熟悉PHP,所以希望有所幫助。

0

我有一個非常類似的問題。我終於可以通過向HTTP頭添加一些元素(「Content-Type」和「Content-Length」)並手動構造url來解決它(所以我可以使用0作爲「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; 
相關問題