我必须在OpenCart网站上通过Google和Facebook实施授权。我对Google rest api没有任何问题,但有些人使用Facebook。 所以,oauth rest api方法非常简单。
<?php
$client_id = APP_ID; // Client ID
$client_secret = APP_SECRET; // Client secret
$redirect_uri = 'https://example.com/facebook-login/index.php'; // Redirect URIs
$url = 'https://www.facebook.com/v2.9/dialog/oauth';
$params = array(
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'response_type' => 'code',
'scope' => 'email'
);
if (isset($_GET['code'])) {
$params = array(
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'client_secret' => $client_secret,
'code' => $_GET['code']
);
$url = 'https://graph.facebook.com/oauth/access_token';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, urldecode(http_build_query($params)));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
$tokenInfo = json_decode($result, true);
echo '<pre>';
print_r($tokenInfo);
echo '</pre>';
if (count($tokenInfo) > 0 && isset($tokenInfo['access_token'])) {
$params = array('access_token' => $tokenInfo['access_token']);
$params = array(
'fields' => 'id,name,email',
'access_token' => $tokenInfo['access_token']
);
$userInfo = json_decode(file_get_contents('https://graph.facebook.com/me' . '?' . urldecode(http_build_query($params))), true);
if (isset($userInfo['id'])) {
echo '<pre>';
print_r($userInfo);
echo '</pre>';
}
}
} else {
$loginUrl = $url . '?' . urldecode(http_build_query($params));
header('Location: ' . filter_var($loginUrl, FILTER_SANITIZE_URL));
}
此代码正常工作,直到你的重定向uri链接不包含OpenCart的“route”参数或类似的东西!
所以,如果我的$ redirect_uri变量等于“https://example.com/facebook-login/”,那就没关系。但是当我尝试在openCart的任何页面上实现这个脚本时(例如“https://example.com/index.php?route=account/login/facebook”)它失败了,因为Facebook oauth api抛出异常:“验证验证码时出错。请确保你的redirect_uri与您在OAuth对话框请求中使用的那个“。
我试图编码斜线但无济于事。那么为什么谷歌休息api可以正常使用这种重定向uri,但Facebook却没有?为什么它有非常严格的规则?
有人可以帮我解决这个问题吗?