【问题标题】:google oauth authentication谷歌oauth认证
【发布时间】:2012-03-09 05:50:04
【问题描述】:

我正在尝试通过授权然后从 google plus 获取数据,但数据变量一直显示“临时移动”的值我做错了什么?

<?php
session_start();

    $client_id = '';
    $client_secret = '';
    $api_key = '';
    $redirect_uri = 'http://localhost:8888/oauth';
    $scope = "https://www.googleapis.com/auth/plus.me";

    if (!isset($_REQUEST['code']))
    {
        $login_path = "https://accounts.google.com/o/oauth2/auth?";
        $login_path .= "redirect_uri=" . $redirect_uri;
        $login_path .= "&response_type=code";
        $login_path .= "&client_id=" . $client_id;
        $login_path .= "&approval_prompt=force";
        $login_path .= "&scope=" . $scope;
        $login_path .= "&access_type=offline";

        echo "<a href='" . $login_path . "'>login</a>";
    }

    else
    {

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/auth");

        curl_setopt($ch, CURLOPT_POST, TRUE);

        // This option is set to TRUE so that the response
        // doesnot get printed and is stored directly in
        // the variable
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        $post_params = "code=" . $_REQUEST['code'] . "&";
        $post_params = "redirect_uri=" . $redirect_uri . "&";
        $post_params .= "client_id=" . $client_id . "&";
        $post_params .= "scope=" . $scope . "&";
        $post_params .= "client_secret=" . $client_secret . "&";
        $post_params .= "grant_type=authorization_code&";
        $post_params .= "response_type=code";


        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);

        $data = curl_exec($ch);

        curl_close($ch);

        //commented this since data was showing empty
        //$data = json_decode($data);

        print '<pre>';
        print $data;
        print '</pre>';
}

【问题讨论】:

  • 不要忘记将该编辑转换为答案并在系统允许时将其标记为已接受。
  • 谢谢我做到了,我想我得等2天才能接受。

标签: php oauth google-api


【解决方案1】:

更新:对于其他有同样问题的人,我想通了。这是我的工作代码。

$client_id = '';
$client_secret = '';
$api_key = '';
$redirect_uri = 'http://localhost:8888/oauth';
$scope = "https://www.googleapis.com/auth/plus.me";
$api_call = "https://www.googleapis.com/plus/v1/people/me?access_token=";

if (!isset($_REQUEST['code']))
{
    $login_path = "https://accounts.google.com/o/oauth2/auth?";
    $login_path .= "redirect_uri=" . $redirect_uri;
    $login_path .= "&response_type=code";
    $login_path .= "&client_id=" . $client_id;
    $login_path .= "&approval_prompt=force";
    $login_path .= "&scope=" . $scope;
    $login_path .= "&access_type=offline";

    echo "<a href='" . $login_path . "'>login</a>";
}

else
{

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token");

    curl_setopt($ch, CURLOPT_POST, TRUE);

    // This option is set to TRUE so that the response
    // doesnot get printed and is stored directly in
    // the variable
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);


    $post_params = "code=" . $_REQUEST['code'] . "&";
    $post_params .= "redirect_uri=" . $redirect_uri . "&";
    $post_params .= "client_id=" . $client_id . "&";
    $post_params .= "scope=" . $scope . "&";
    $post_params .= "client_secret=" . $client_secret . "&";
    $post_params .= "grant_type=authorization_code&";

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);

    $data = curl_exec($ch);

    curl_close($ch);

    $data = json_decode($data);

    $access_token = $data->access_token;
    $refresh_token = $data->refresh_token;

    // end of oauth now call google plus api. not $api_call holds the request uri value
    $call = $api_call . $access_token;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $call);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $out = curl_exec($ch);

    echo '<pre>';
    print_r($out);
    echo '</pre>';

}

【讨论】:

    猜你喜欢
    • 2018-03-07
    • 1970-01-01
    • 2019-11-02
    • 2023-03-05
    • 2021-10-02
    • 2023-03-17
    • 2013-07-12
    • 2014-02-22
    相关资源
    最近更新 更多