[英]How do I reconnect my web pages on my website after updating to PHP 7 with a MySQL database 5.0.0?<?
[英]After successfully connecting to Google Contacts API via php, how/what do I store in database to reconnect later?
所以这是交易。
我已通过php连接到Google联系人的api,将所有内容存储在会话中,并检索了联系人列表。
我想要的是将所有必要的令牌存储在数据库中,并在以后检索它们以在同一用户上重复使用它们。 我无法弄清楚要存储什么信息...我试图将会话中的每个小项目都存储到数据库中,并在尝试重新连接到api时重新加载它,但是由于我的令牌不是不正确。
我想对于熟悉OAuth的人来说,答案很简单-实际上只是我要存储什么的问题。
代码如下:
<?php
session_start();
include_once "../oauth-php/library/OAuthStore.php";
include_once "../oauth-php/library/OAuthRequester.php";
global $db;
$userid=$_SESSION['userid'];
define("GOOGLE_CONSUMER_KEY", "website.com"); //
define("GOOGLE_CONSUMER_SECRET", "----------------------"); //
define("GOOGLE_OAUTH_HOST", "https://www.google.com");
define("GOOGLE_REQUEST_TOKEN_URL", GOOGLE_OAUTH_HOST . "/accounts/OAuthGetRequestToken");
define("GOOGLE_AUTHORIZE_URL", GOOGLE_OAUTH_HOST . "/accounts/OAuthAuthorizeToken");
define("GOOGLE_ACCESS_TOKEN_URL", GOOGLE_OAUTH_HOST . "/accounts/OAuthGetAccessToken");
define('OAUTH_TMP_DIR', function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : realpath($_ENV["TMP"]));
// Init the OAuthStore
$options = array(
'consumer_key' => GOOGLE_CONSUMER_KEY,
'consumer_secret' => GOOGLE_CONSUMER_SECRET,
'server_uri' => GOOGLE_OAUTH_HOST,
'request_token_uri' => GOOGLE_REQUEST_TOKEN_URL,
'authorize_uri' => GOOGLE_AUTHORIZE_URL,
'access_token_uri' => GOOGLE_ACCESS_TOKEN_URL
);
OAuthStore::instance("Session", $options);
try
{
// STEP 1: If we do not have an OAuth token yet, go get one
if (empty($_GET["oauth_token"]))
{
$getAuthTokenParams = array('scope' =>
'https://www.google.com/m8/feeds/',
'xoauth_displayname' => 'My web app',
'oauth_callback' => 'http://website.com/google.php');
// get a request token
$tokenResultParams = OAuthRequester::requestRequestToken(GOOGLE_CONSUMER_KEY, 0, $getAuthTokenParams);
// redirect to the google authorization page, they will redirect back
header("Location: " . GOOGLE_AUTHORIZE_URL . "?btmpl=mobile&oauth_token=" . $tokenResultParams['token']);
}
else {
// STEP 2: Get an access token
$oauthToken = $_GET["oauth_token"];
$oauthVerifier = $_GET["oauth_verifier"];
$tokenResultParams = $_GET;
//$db->query("UPDATE gmkeys SET token='$oauthToken', secrettoken='$oauthVerifier'");
try {
OAuthRequester::requestAccessToken(GOOGLE_CONSUMER_KEY, $oauthToken, 0, 'POST', $_GET);
}
catch (OAuthException2 $e)
{
var_dump($e);
// Something wrong with the oauth_token.
// Could be:
// 1. Was already ok
// 2. We were not authorized
return;
}
// make the request.
$request = new OAuthRequester("https://www.google.com/m8/feeds/contacts/default/full?max-results=1000&group=http%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds%2Fgroups%2Fusernamehere%40gmail.com%2Fbase%2F6", 'GET', $tokenResultParams);
$result = $request->doRequest(0);
if ($result['code'] == 200)
{
$xml = new SimpleXMLElement($result['body']);
...
}
else
{
echo 'Error';
}
}
}
catch(OAuthException2 $e) {
echo "OAuthException: " . $e->getMessage();
var_dump($e);
}
?>
access_token和request_token字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.