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

get text from page and put it into a variable

本文关键字:变量 取文本 获取      更新时间:2023-09-26

我想使用以下方法扩展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消除为一个新变量,并将其存储到我的数据库中。

明白了。我使用了jquery并提供了url(extendingUrl),作为回报,它给了我请求的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);
});