如何将保存到DOM的foursquare access_token作为链接提取,以便将其用于API调用?

时间:2022-09-07 10:11:26

Essentially, if I'm in Firebug and look at the net objects I see the status 200

基本上,如果我在Firebug中并查看网络对象,我会看到状态200

If I click on the JSON tab I can see my access_token, but how do I extract it from there so I can use for API calls?

如果我单击JSON选项卡,我可以看到我的access_token,但是如何从那里提取它,以便我可以用于API调用?

Here's the latest code tried.

这是尝试过的最新代码。

var jsonUrl = url +"&callback=?";
var access_token;

$("#getJSON").click(function() {
    $.getJSON(jsonUrl, { dataType: "JSONP" }, function(json){ 
        ...
        access_token = json.access_token;
        ...
    });
});

also tried:

还尝试过:

$.ajax({
  dataType: 'jsonp',
  jsonp: 'callback',
  url: url,
  success: function (json) {
    console.log(json.access_token);
  },
});

But when I try and alert(access_token); or run a foursquare api call I get the following errors

但是当我尝试并提醒时(access_token);或运行foursquare api调用我得到以下错误

Resource interpreted as Script but transferred with MIME type application/json. 

Uncaught SyntaxError: Unexpected token : checkinsGET https://api.foursquare.com/v2/users/self/checkins?oauth_token=undefined&format=json 401 (Unauthorized)

I feel like its ready and waiting for me to call it, but how on earth do I print it from the Dom into a var that I can use? Been fighting for hours and been trying all my research techniques for some reason this one's elluding me. Thanks for everyone's help so far, I'm really hoping to get passed this!

我觉得它准备就绪并等着我来打电话,但是我怎么把它从Dom打印到我可以使用的var中呢?几个小时都在争吵,并且由于某种原因,我一直在尝试我的所有研究技巧。感谢大家的帮助到目前为止,我真的希望能够通过这个!

1 个解决方案

#1


1  

Browsers natively implement a global function JSON.parse(), and if for some reason that doesn't work you can use jQuery's parseJSON() function.

浏览器本身实现了一个全局函数JSON.parse(),如果由于某种原因无效,你可以使用jQuery的parseJSON()函数。

Here's how it works. Let's say your JSON object has the following format:

这是它的工作原理。假设您的JSON对象具有以下格式:

{
    Status: "Success",
    Resource: {
        Name: "Person's Name",
        URL: "http://blahblahblah.com/extrastuffhere?querystuff=here"}}

Then you can access the "URL" element by using JSON.parse() like so:

然后你可以使用JSON.parse()来访问“URL”元素,如下所示:

var url = JSON.parse(json).Resource.URL;

(I'm unfamiliar with Foursquare but it should look similar, and you can do console.log(json) to see its structure.)

(我对Foursquare不太熟悉,但它应该看起来很相似,你可以用console.log(json)查看它的结构。)

So your code might look something like this:

所以你的代码看起来像这样:

$("#getJSON").click(function() {
    $.getJSON(jsonUrl, { dataType: "JSONP" }, function(json){ 
        var parsed = JSON.parse(json);
        console.log(parsed);
        access_token = parsed.access_token;
    });
});

Hope that helps!

希望有所帮助!

#1


1  

Browsers natively implement a global function JSON.parse(), and if for some reason that doesn't work you can use jQuery's parseJSON() function.

浏览器本身实现了一个全局函数JSON.parse(),如果由于某种原因无效,你可以使用jQuery的parseJSON()函数。

Here's how it works. Let's say your JSON object has the following format:

这是它的工作原理。假设您的JSON对象具有以下格式:

{
    Status: "Success",
    Resource: {
        Name: "Person's Name",
        URL: "http://blahblahblah.com/extrastuffhere?querystuff=here"}}

Then you can access the "URL" element by using JSON.parse() like so:

然后你可以使用JSON.parse()来访问“URL”元素,如下所示:

var url = JSON.parse(json).Resource.URL;

(I'm unfamiliar with Foursquare but it should look similar, and you can do console.log(json) to see its structure.)

(我对Foursquare不太熟悉,但它应该看起来很相似,你可以用console.log(json)查看它的结构。)

So your code might look something like this:

所以你的代码看起来像这样:

$("#getJSON").click(function() {
    $.getJSON(jsonUrl, { dataType: "JSONP" }, function(json){ 
        var parsed = JSON.parse(json);
        console.log(parsed);
        access_token = parsed.access_token;
    });
});

Hope that helps!

希望有所帮助!