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

get text from page and put it into a variable

我想使用以下方法扩展 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

该页面将 return 类似于:

access_token=CAAERkjuisOYBALHbBZB9oq01ybCoyBfyGlSHtkkZBDqDvevrWZC42JwMawxxxOxQsiKHMNVPHZCbh3ntF7GdnYwnq3BLTh6ZA2YUJCVSh8QA5nEZACZCXVtZCL5RZC72pl3afKMAOMG2WGKtjnD1GJTjQEPC2XH3X1ycr3GeAUWBShDj7ojFVCWhDe6jBGvBu2nn7Ohu9C2udBoamOBxoQFun&expires=5182005

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

知道了。我使用 jquery 并在 return 中输入 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);
});