【问题标题】:Google API OAuth 2.0 CURL returning “Required parameter is missing: response_type”Google API OAuth 2.0 CURL 返回“缺少必需参数:response_type”
【发布时间】:2015-07-13 01:48:17
【问题描述】:

我关注了这个topic。我的目的是我想用 php 和 curl 登录谷歌,以便在登录和批准后获得授权码的批准访问申请。

我想创建脚本以向博主添加新帖子。

这是我的代码:

$USERNAME = 'mygmail@gmail.com';
$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;
}

我有错误消息。

Error: invalid_request
Required parameter is missing: response_type

当我看到请求网址时。它有参数“response_type”。

我该怎么做?

对不起。我英语不好。

【问题讨论】:

    标签: php curl


    【解决方案1】:

    您将参数放在错误的位置。

    curl_setopt($ch, CURLOPT_URL,$request_to);
    

    应该是:

    curl_setopt($ch, CURLOPT_URL,$url);
    

    您似乎不小心删除了这里的一些选项:

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

    更新

    我还没有收到第二个请求,第一个看起来很乱。

    您似乎复制并粘贴了一堆无意义的代码。

    参数没有意义。

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

    这是清理第一个请求的 curl 选项的最佳方法:

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

    【讨论】:

    • 感谢您的回答。但你的建议不起作用。如果我将 $request_to 更改为 $url.它显示错误消息“缺少必需的参数:response_type”。
    • 感谢您的更新。我是根据你最后的建议做到的。现在我可以访问登录页面了。之后,我创建了登录谷歌的脚本,但出现了错误。
    猜你喜欢
    • 2016-12-14
    • 2012-01-02
    • 2015-09-29
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 2016-04-14
    • 1970-01-01
    相关资源
    最近更新 更多