【问题标题】:What is wrong with my foursquare api call?我的foursquare api调用有什么问题?
【发布时间】:2016-05-03 18:58:44
【问题描述】:

现场示例在这里 http://kenziejoy.github.io/frontend-nanodegree-map/

我正在尝试提取有关我在数组中硬编码的位置的数据 - 通过它们的四方 id(似乎没有工作)或它们的纬度和经度。 (客户端 ID 和密码是变量,我只是没有在这里显示)

除了从他们的数据库中提取数据以显示在地图上之外,我不需要任何其他功能,所以我认为它属于无用户访问权限,但它给了我一个错误,即请求不正确,因为我没有有适当的身份验证。

提前致谢

来自foursquare网站 "无用户访问

我们的一些与特定用户信息无关的端点(例如场所搜索)已启用无用户访问(这意味着您无需让用户授权您的应用即可访问)。要发出无用户请求,请在请求 URL 中指定您的使用者密钥的客户端 ID 和机密,而不是身份验证令牌。

https://api.foursquare.com/v2/venues/search?ll=40.7,-74&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&v=YYYYMMDD

要查看每个端点需要什么级别的权限,请查看端点页面顶部的过滤器。”

        /**********FourSquare***************/
    $.ajax({
        url:'https://api.foursquare.com/v2/venues/search',
        dataType: 'json',
        data: 'limit=1' +
                '&ll='+ placeItem.lat() +','+ placeItem.lng() +
                '&?client_id='+ CLIENT_ID +
                '&client_secret='+ CLIENT_SECRET +
                '&v=20140806' +
                '&m=foursquare',
        async: true,

        success: function (data) {
            var result = data.response.venue;
            var contact = result.hasOwnProperty('contact') ? result.contact : '';
            if (contact.hasOwnProperty('formattedPhone')) {
                placeItem.phone(contact.formattedPhone || '');
            }

            var location = result.hasOwnProperty('location') ? result.location : '';
            if (location.hasOwnProperty('address')) {
                placeItem.address(location.address || '');
            }

            var bestPhoto = result.hasOwnProperty('bestPhoto') ? result.bestPhoto : '';
            if (bestPhoto.hasOwnProperty('prefix')) {
                placeItem.photoPrefix(bestPhoto.prefix || '');
            }

            if (bestPhoto.hasOwnProperty('suffix')) {
                placeItem.photoSuffix(bestPhoto.suffix || '');
            }

            var description = result.hasOwnProperty('description') ? result.description : '';
            placeItem.description(description || '');

            var rating = result.hasOwnProperty('rating') ? result.rating : '';
            placeItem.rating(rating || 'none');

            var url = result.hasOwnProperty('url') ? result.url : '';
            placeItem.url(url || '');

            placeItem.canonicalUrl(result.canonicalUrl);

            // Infowindow code is in the success function so that the error message

            // Content of the infowindow
            var contentString = '<div id="iWindow"><h4>' + placeItem.name() + '</h4><div id="pic"><img src="' +
                    placeItem.photoPrefix() + '110x110' + placeItem.photoSuffix() +
                    '" alt="Image Location"></div><p>Information from Foursquare:</p><p>' +
                    placeItem.phone() + '</p><p>' + placeItem.address() + '</p><p>' +
                    placeItem.description() + '</p><p>Rating: ' + placeItem.rating() +
                    '</p><p><a href=' + placeItem.url() + '>' + placeItem.url() +
                    '</a></p><p><a target="_blank" href=' + placeItem.canonicalUrl() +
                    '>Foursquare Page</a></p><p><a target="_blank" href=https://www.google.com/maps/dir/Current+Location/' +
                    placeItem.lat() + ',' + placeItem.lng() + '>Directions</a></p></div>';

            // Add infowindows
                google.maps.event.addListener(placeItem.marker, 'click', function () {
                infowindow.open(map, this);
                // Bounce animation
                placeItem.marker.setAnimation(google.maps.Animation.BOUNCE);
                setTimeout(function () {
                    placeItem.marker.setAnimation(null);
                }, 800);
                infowindow.setContent(contentString);
            });
    },

        // Alert the user on error.
        error: function (e) {
            infowindow.setContent('<h5>Foursquare data is unavailable.</h5>');
            document.getElementById("error").innerHTML = "<h4>Foursquare data is unavailable. Please try refreshing.</h4>";
    }
    });

【问题讨论】:

    标签: javascript ajax foursquare


    【解决方案1】:

    我查看了实时示例 URL,您在 Chrome 的 JavaScript 控制台中遇到了很多错误的请求错误。

    看看这些,你有一个错误的 URL,你正在使用:

    https://api.foursquare.com/v2/venues/search?limit=1&ll=45.5589522,-122.6517163&?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&v=20140806&m=foursquare
    

    问题似乎是你有:

     &?client_id
    

    这会使 URL 无效。

    改成

     &client_id
    

    解决了这个问题,然后我看到来自 Foursquare 的数据。

    【讨论】:

    • 你是男人中的王子!
    猜你喜欢
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    相关资源
    最近更新 更多