【问题标题】:Unsupported content with type: multipart/form-data in google plus api oauth2类型不受支持的内容:google plus api oauth2 中的 multipart/form-data
【发布时间】:2015-11-13 11:36:41
【问题描述】:

我正在尝试在我的网络应用中使用 google plus Api 登录。

我从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" }

我正在将我的代码 sn-ps 用于执行请求(作为客户端 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;
}

【问题讨论】:

    标签: api http google-api google-plus google-api-php-client


    【解决方案1】:

    我有一个非常相似的问题。我终于能够通过向 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;
    

    【讨论】:

      【解决方案2】:

      我在使用 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,所以希望对您有所帮助。

      【讨论】:

        【解决方案3】:

        您的请求是正确的,但缺少一些东西 内容类型不是multipart/form-data 应该是application/x-www-form-urlencoded

        【讨论】:

          【解决方案4】:
                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;
          }
          

          【讨论】:

            猜你喜欢
            • 2018-12-22
            • 2018-06-11
            • 2018-08-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-26
            • 1970-01-01
            相关资源
            最近更新 更多