代码之家  ›  专栏  ›  技术社区  ›  Pablo Abreu

如何获得优步骑行价格

  •  -1
  • Pablo Abreu  · 技术社区  · 8 年前

    我编辑了整个主题,因为我对整个API进行了很好的阅读,现在我遇到了另一个问题。。。我会尽量在这里留下更多的细节。

    你给出的错误是优步api的401,但是,我做的一切都是对的。。。我的索引。html如下所示:

    指数html

    <html>
    <head>
        <title>Party Invitation</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" href="css/normalize.css">
        <link rel="stylesheet" href="css/style.css">
    </head>
    <body>
        <section>
            <h1>Party</h1>
            <div class="info-container">
                <span class="info-header">When</span>
                <span class="info when">September 18, 2014 at 6:00pm</span>
            </div>
            <div class="info-container">
                <span class="info-header">Where</span>
                <span class="info where">Thinkful HQ</span>
            </div>
            <img class="map" src="http://maps.googleapis.com/maps/api/staticmap?center=40.7248,-73.99597&zoom=17&format=png&sensor=false&size=280x280&maptype=roadmap&style=element:geometry.fill|color:0xf4f4f4&markers=color:red|40.7248,-73.99597&scale=2" alt="">
            <div class="button">
                <p id="time">ESTIMATING TIME</p>
            </div>
        </section>
    
    <script src="js/jquery-2.1.1.min.js"></script>
    <script src="js/uber.js"></script>
    </body>
    

    然后是我的优步。js是这样的:

    优步。js公司

        // Uber API Constants
    var uberClientId = "RxGbzH***************"
        , uberServerToken = "h_hqd3L4***************";
    
    // Create variables to store latitude and longitude
    var userLatitude
        , userLongitude
      , partyLatitude = 19.3256725
      , partyLongitude = -43.1579731;
    
    
    
    navigator.geolocation.watchPosition(function(position) {
        // Update latitude and longitude
        userLatitude = position.coords.latitude;
        userLongitude = position.coords.longitude;
    
      // Query Uber API if needed
        getEstimatesForUserLocation(userLatitude, userLongitude);
    });
    
    function getEstimatesForUserLocation(latitude,longitude) {
      $.ajax({
        url: "https://api.uber.com/v1/estimates/price",
        headers: {
            Authorization: "KA.ey**********************************************************************************************************************************" + uberServerToken
        },
        data: { 
          start_latitude: latitude,
          start_longitude: longitude,
          end_latitude: partyLatitude,
          end_longitude: partyLongitude
        },
        success: function(result) {
          console.log(result);
        }
      });
    }
    

    And this is the error that is appearing

    And my uber control panel is like this

    有人能告诉我为什么他们在控制台上给出这两个错误吗?我不知道该怎么办

    =\

    1 回复  |  直到 8 年前
        1
  •  0
  •   Sasa Jovanovic    8 年前

    正如我所看到的,您的应用程序设置不正确。您没有重定向URL和隐私策略,并且因为您正在从JS进行API调用-源URL:

    enter image description here

    enter image description here

    所以添加如下内容:

    重定向URL: http://localhost:7000/redirect
    隐私政策: http://localhost:7000/privacy
    源URL: http://localhost:7000

    我不知道你从哪里获得了访问令牌-因为你没有正确设置你的应用程序-但即使这样,这也不会起作用,因为你的头无效。您缺少access\u令牌的令牌类型“Bearer”。因此,在这种情况下,您的代码应该如下所示:

    headers: {
        Authorization: "Bearer KA.ey**********************************************************************************************************************************"
        }
    

    如果要使用服务器令牌:

    headers: {
        Authorization: "Token uberServerToken"
        }
    

    请阅读我们的 documentation 了解更多信息。