谷歌oauth认证 [英] google oauth authentication

查看:117
本文介绍了谷歌oauth认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过授权,然后从谷歌加数据,但数据变量不断显示暂时移动的值我做错了什么?

 <?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 =。 $范围;
$ login_path。=& access_type = offline;

echo< a href ='。 $ login_path。 >登录< / A>中;
}

else
{

$ ch = curl_init();

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

curl_setopt($ ch,CURLOPT_POST,TRUE);

//此选项设置为TRUE,以便响应
//不打印并直接存储在
中//变量
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 =。 $范围。 &安培;;
$ 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);

//评论这一点,因为数据显示为空
// $ data = json_decode($ data);

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


解决方案

更新:我想到了同样的问题。这是我的工作代码。

  $ 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 =。 $范围;
$ login_path。=& access_type = offline;

echo< a href ='。 $ login_path。 >登录< / A>中;
}

else
{

$ ch = curl_init();

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

curl_setopt($ ch,CURLOPT_POST,TRUE);

//此选项设置为TRUE,以便响应
//不打印并直接存储在
中//变量
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 =。 $范围。 &安培;;
$ 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;

//结束oauth现在调用google plus api。不是$ api_call持有请求uri值
$ 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>';

}


i am trying to pass the authorization to then get data from google plus but the data variable keeps showing a value of "moved temporarily" what am i doing wrong?

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

解决方案

Update: For anyone else having the same problem i figured it out. here is my working code.

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

}

这篇关于谷歌oauth认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆