Facebook的服务器端身份验证与经典ASP
问题描述:
类似this closed question,但我会让我的更精确,所以希望就会有一个答案:)Facebook的服务器端身份验证与经典ASP
我加入Facebook登录按钮,一个网站,一切都在经典ASP,我是Facebook SDK/API的新手,https://developers.facebook.com/docs/authentication在PHP中提供了它的所有代码示例。我需要通过一些COM函数将一些FB用户信息传递给服务器的数据库,所以我将使用服务器端身份验证。到现在为止还挺好。服务器端身份验证不给我直接访问令牌虽然它给了我一个代码,我大多了解如何将其转换成访问令牌中的PHP示例,
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = @file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
我真的不知道ASP/VBScript的方式来做任何上述。 PHP有比ASP那么多的内置功能,如urlencode
,file_get_contents
,一个parse_str
这是足够聪明,像对待查询参数的字符串,我甚至不知道如果我想需要一个像json_decode
或没有什么!
对于的file_get_contents我想尝试像
fbApiCode = Request.QueryString("code")
if len(fbApiCode) > 0 then
set xmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
appAuth = "https://graph.facebook.com/oauth/access_token?client_id=" & _
Application("fbAppId") & "&redirect_uri=" & Application("fbRedirectUri") & "&client_secret=" _
Application("fbAppSecret") & "&code=" & fbApiCode
xmlHttp.open("GET", appAuth, false) ' or post?
accessResponse = xmlHttp.responseText
这甚至接近正确的做法还是我跑下来一个兔子洞?如果我不能用VBScript将代码翻译成访问令牌,那么我将不得不使用JavaScript与FB.getLoginStatus()
来获取FB用户的信息(姓名,电子邮件等),然后用AJAX将其发送到我的服务器,我知道是不是非常安全,所以我想避免诉诸这一点。
答
我知道这是一个古老的线程,但这种解决方案可以帮助别人。
您需要申请使用该代码的令牌,然后请求与令牌的用户信息。
<script language="javascript" runat="server">
"use strict";
function getJSON(strOrObject){
if (typeof strOrObject == "string") {
return eval("(" + strOrObject + ")");
}
return strOrObject;
}
</script>
<%
fbClientToken = [[CLIENTTOKEN]]
fbAppID = [[APPID]]
fbAppSecret = [[APPSECRET]]
fbAuthURL = "https://www.facebook.com/dialog/oauth"
fbTokenURL = "https://graph.facebook.com/v2.3/oauth/access_token"
fbOAuthCallback = [[CALLBACKURL]]
fbScope = "public_profile,email"
if request("go")="login" then
Randomize
nonce = Rnd
response.redirect fbAuthURL & "?response_type=code&client_id=" & fbAppID & "&redirect_uri=" & Server.URLEncode(fbOAuthCallback) & "&scope=" & fbScope & "&state=" & nonce
else
if request("code") <> "" then
information = "client_id=" & fbAppID & "&redirect_uri=" & Server.URLEncode(fbOAuthCallback) & "&client_secret=" & fbAppSecret & "&code=" & request("code")
Set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "GET", fbTokenURL & "?" & information, false
xmlhttp.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
xmlhttp.send
respText = xmlhttp.ResponseText
if InStr(respText,"access_token") then
dim respJSON : set respJSON = getJSON(respText)
getURL = "https://graph.facebook.com/me?access_token=" & respJSON.access_token
xmlhttp.Open "GET", getURL, false
xmlhttp.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
xmlhttp.send
respToken = xmlhttp.ResponseText
%><h1>Token Inspection Response</h1><%
if InStr(respToken,"{") and InStr(respToken,"}") then
dim userJSON : set userJSON = getJSON(respToken)
response.write "<br/>ID: " & userJSON.id
response.write "<br/>email: " & userJSON.email
response.write "<br/>first_name: " & userJSON.first_name
response.write "<br/>last_name: " & userJSON.last_name
response.write "<br/>name: " & userJSON.name
response.write "<br/>gender: " & userJSON.gender
response.write "<br/>locale: " & userJSON.locale
response.write "<br/>verified: " & userJSON.verified
response.write "<br/>link: " & userJSON.link
response.write "<br/>timezone: " & userJSON.timezone
else
%><h1>Invalid Inspection Response</h1><%=respToken%><%
end if
else
%><h1>Invalid Response</h1><%=respText%><%
end if
end if
end if
%>
什么类型的web应用程序的这是什么?页面标签应用程序?画布应用程序?一个独立的网站? – DMCS 2012-01-28 00:01:16
“人们仍然使用经典的ASP?”是。 – Dee 2012-01-28 20:29:50
独立网站,其自己的注册/登录,我需要与Facebook登录集成。允许新老用户使用Facebook登录是我们计划使用的唯一Facebook API功能。 (是的,这是很多遗留代码;直到现在,一些控制reg/login的代码自2004年以后不需要任何更改!) – 2012-01-30 14:04:03