代码之家  ›  专栏  ›  技术社区  ›  Mustafa

Yelp API HTTP请求授权承载器

  •  0
  • Mustafa  · 技术社区  · 6 年前

    大家晚上好。我是API的初学者,需要Yelp的Fusion API的帮助。我认为我的授权变量/头有一些错误。我运行请求并得到404错误。有人能解释清楚吗?

    您可以在此链接上找到有关Yelp API的更多详细信息: https://www.yelp.com/developers/documentation/v3/business_search

    import requests
    
    url = 'https://api.yelp.com/v3/businesses/search/';
    header = {'Authorization':'Bearer MYLONGTOKENx'}
    
    response = requests.get(url, headers = header)
    
    print(response.status_code)
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Ilan P    6 年前

    请参阅下面的示例,用您的API密钥交换密钥(XXXXXXXXXXX);在我认为它将帮助您之前,我为某人编写了这个示例;在这个示例中,我们将对Yelp的业务搜索API进行安全调用,并返回JSON;在一个简单的示例中分析它并使用它的值;希望它有帮助!

    <!doctype html>
    <html lang="en">
       <head>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
          <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
          <title>Ilan's Test</title>
       </head>
       <body>
    
       <div class="container">
            <div class="row">
                <div class="col-lg-12">
                    <div id="results">
    
                    </div>
                </div>
             </div>
       </div>
    
          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
          <script>
             var myurl = "https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=by-chloe&location=boston";
    
             $.ajax({
                url: myurl,
                headers: {
                 'Authorization':'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
             },
                method: 'GET',
                dataType: 'json',
                success: function(data){
                    // Grab the results from the API JSON return
                    var totalresults = data.total;
                    // If our results are greater than 0, continue
                    if (totalresults > 0){
                        // Display a header on the page with the number of results
                        $('#results').append('<h5>We discovered ' + totalresults + ' results!</h5>');
                        // Itirate through the JSON array of 'businesses' which was returned by the API
                        $.each(data.businesses, function(i, item) {
                            // Store each business's object in a variable
                            var id = item.id;
                            var alias = item.alias;
                            var phone = item.display_phone;
                            var image = item.image_url;
                            var name = item.name;
                            var rating = item.rating;
                            var reviewcount = item.review_count;
                            var address = item.location.address1;
                            var city = item.location.city;
                            var state = item.location.state;
                            var zipcode = item.location.zip_code;
                            // Append our result into our page
                            $('#results').append('<div id="' + id + '" style="margin-top:50px;margin-bottom:50px;"><img src="' + image + '" style="width:200px;height:150px;"><br>We found <b>' + name + '</b> (' + alias + ')<br>Business ID: ' + id + '<br> Located at: ' + address + ' ' + city + ', ' + state + ' ' + zipcode + '<br>The phone number for this business is: ' + phone + '<br>This business has a rating of ' + rating + ' with ' + reviewcount + ' reviews.</div>');
                      });
                    } else {
                        // If our results are 0; no businesses were returned by the JSON therefor we display on the page no results were found
                        $('#results').append('<h5>We discovered no results!</h5>');
                    }
                }
             });      
    
          </script>
       </body>
    </html>
    
    推荐文章