从页面获取文本并将其放入变量中

时间:2015-03-05 03:04:50

标签: javascript facebook facebook-access-token

我想使用以下方法扩展facebook访问令牌:

function extend(fb_access_token) {
	var extendingUrl;
		
	try{
		console.log("Extending Facebook Access Token.");
		
		if (app_id == "" || app_id == null) {
			alert("App ID not configured properly.");
			hasError = true;
			return;
		} else {
			hasError = false;
		}
		
		if (app_secret == "" || app_secret == null) {
			alert("App Secret not configured properly.");
			hasError = true;
			return;
		} else {
			hasError = false;
		}

		if (fb_access_token == "" || fb_access_token == null) {
			alert("Facebook Access Token not was not generated.");
			hasError = true;
			return;
		} else {
			hasError = false;
		}
		
		if(hasError) {
			alert("URL not formed.");
		} else {
			extendingUrl = "https://graph.facebook.com/oauth/access_token?client_id="+app_id+"&client_secret="+app_secret+"&grant_type=fb_exchange_token&fb_exchange_token="+fb_access_token;
			window.location.replace = extendingUrl;
			console.log("Facebook Access Token successfully extended.");
		}
		
	} catch (OAuthException) {
		console.log("Login status or access token has expired, been revoked, or is otherwise invalid.");
	}
}

我想从最终会提供扩展访问令牌的页面中获取生成的访问令牌,请参阅var extendingUrl

该页面将返回如下内容:

access_token=CAAERkjuisOYBALHbBZB9oq01ybCoyBfyGlSHtkkZBDqDvevrWZC42JwMawxxxOxQsiKHMNVPHZCbh3ntF7GdnYwnq3BLTh6ZA2YUJCVSh8QA5nEZACZCXVtZCL5RZC72pl3afKMAOMG2WGKtjnD1GJTjQEPC2XH3X1ycr3GeAUWBShDj7ojFVCWhDe6jBGvBu2nn7Ohu9C2udBoamOBxoQFun&expires=5182005

我将对上面的字符串进行子字符串处理,并将access_token=&expires=5182005删除为新变量,并将其存储到我的数据库中。

1 个答案:

答案 0 :(得分:0)

知道了。我使用了jquery并提供了url(extendsUrl),作为回报,它给了我请求的url的内容。然后我使用正则表达式和子字符串来消除不需要的文本。

$.get(extendingUrl, function(data) {
  var raw = data;
  var patt1 = /(access_token=)(.*)(?=&expires=)/;
  var result = raw.match(patt1);
  var longToken = result[0].substring(13, 400);
});