Yelp API HTTP 请求授权承载 [英] Yelp API HTTP Request Authorization Bearer

查看:44
本文介绍了Yelp API HTTP 请求授权承载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家晚上好.我一般是 API 的初学者,需要一些有关 Yelp 的 Fusion API 的帮助.我认为我的授权变量/标题有一些错误.我运行请求并收到 404 错误.有人可以提供一些说明吗?

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

导入请求url = 'https://api.yelp.com/v3/businesses/search/';header = {'Authorization':'Bearer MYLONGTOKENx'}response = requests.get(url, headers = header)打印(响应.status_code)

解决方案

看下面的例子,把key (xxxxxxxxxxxx) 换成你的api key;在我认为它会帮助你之前,我为某人写了这个例子;在这个例子中,我们将安全调用 Yelp 的商业搜索 API 并返回 JSON;解析它并在一个简单的例子中使用它的值;希望有帮助!

<html lang="zh-cn"><头><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/Z5nLIefveQRIY9nfy6xoR1uRYBtp9Zg9Dvg3"匿名"><title>伊兰的测试</title><身体><div class="容器"><div class="row"><div class="col-lg-12"><div id="结果">

<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="匿名"></script><脚本>var myurl = "https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=by-chloe&location=boston";$.ajax({网址:我的网址,标题:{'Authorization':'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',},方法:'获取',数据类型:'json',成功:功能(数据){//从 API JSON 返回中获取结果var totalresults = data.total;//如果我们的结果大于 0,则继续如果(总结果 > 0){//在页面上显示一个带有结果数量的标题$('#results').append('<h5>我们发现了 ' + totalresults + ' results!</h5>');//遍历 API 返回的 'businesses' JSON 数组$.each(data.businesses, function(i, item) {//将每个业务的对象存储在一个变量中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;//将我们的结果附加到我们的页面中$('#results').append('<div id="' + id + '" style="margin-top:50px;margin-bottom:50px;"><img src="' + image+ '" style="width:200px;height:150px;"><br>我们找到了 <b>' + name + '</b> (' + alias + ')<br>商家 ID: ' + id + '
位于:' + 地址 + ' ' + 城市 + ', ' + 州 + ' ' + 邮政编码 + '<br>此商家的电话号码是:' + 电话 +'<br>此商家的评分为 ' + rating + ' 和 ' + reviewcount + ' 个评论.</div>');});} 别的 {//如果我们的结果是 0;JSON 未返回任何商家,因此我们在页面上显示未找到任何结果$('#results').append('<h5>我们没有发现任何结果!</h5>');}}});</html>

Good evening everyone. I'm a beginner to API's in general and need some help with Yelp's Fusion API. I think there is some error with my Authorization Variable/Header. I run the request and get a 404 error. Can someone provide some clarity?

You can find more details on Yelp API on this link: 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)

解决方案

See the example below, swap the key (xxxxxxxxxxxx) with your api key; I wrote this example for someone before I figure it will help you; in this example we'll make a secure call to Yelp's Business Search API and return back the JSON; parse it and use its values in a simple example; hope it helps!

<!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>

这篇关于Yelp API HTTP 请求授权承载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆