Lyft-API-从Localhost获取 [英] Lyft-API - GET from Localhost

查看:89
本文介绍了Lyft-API-从Localhost获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图弄清楚如何使这个Vue项目与Lyft API一起使用.我已经能够从三足的程序中成功创建一个身份验证令牌,但是我无法从localhost:8080获取可用的驱动器类型https://api.lyft.com/v1/ridetypes端点.它可以在 Postman 上工作.

I have been trying to figure out how to get this Vue project to work with the Lyft API. I have been able to get an Auth Token successfully created from the three-legged procedure, but I am unable to get the available drive types https://api.lyft.com/v1/ridetypes endpoint from the localhost:8080. It does work on Postman.

它一直在说明:

在以下位置访问XMLHttpRequest ' https://api.lyft.com/v1/ridetypes ?lat = 37.7752315& lng = -122.418075 ' 来自来源" http://localhost:8080 "已被CORS政策阻止: 对预检请求的响应未通过访问控制检查:否 请求中存在"Access-Control-Allow-Origin"标头 资源.

Access to XMLHttpRequest at 'https://api.lyft.com/v1/ridetypes?lat=37.7752315&lng=-122.418075' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我曾尝试使用vue.config.js文件做代理:

I had tried doing a proxy using a vue.config.js file:

module.exports = {
    devServer: {
        proxy: {
            '/lyftapi': {
                target: 'https://api.lyft.com/v1',
                ws: true,
                changeOrigin: true
            }
        }
    }
}

我到过Stack Overflow的其他地方,这是与我的问题最接近的事情,但没有答案.

I been around other parts of Stack Overflow, and this is the closest thing to my problem, but no answers.

Lyft API中的CORS错误最近开始

有什么建议吗?

Axios拨打电话

axios.get('/ridetypes', {
    baseURL: 'https://api.lyft.com/v1',
    headers: {
        'Authorization': this.lyftToken,
    },
    params: {
        lat: lat.toString(),
        lng: long.toString()
    }
})

如果有任何意义,我能够成功进行GET调用以检索Uber产品,但不能提供太多的Auth Token(除非它来自Postman).

If it means anything, I am able to make successful GET calls to retrieve Uber products, but not so much the Auth Token (unless its from Postman).

推荐答案

Lyft-API已禁用CORS,这意味着浏览器将阻止对api.lyft.com的调用.

Lyft-API has disabled CORS, this means that browsers will block calls to api.lyft.com.

Vue无法对此进行任何处理,因为这是浏览器安全策略.

Vue won't be able to do anyting about this as this is a browser security policy.

幸运的是,您无法阻止自己从自己的服务器发出呼叫.

Luckily there is nothing from stoping you to make this call from your own server.

一种解决方案是使用您自己的服务器转发请求和响应.您对服务器进行呼叫,服务器对lyft进行呼叫,等待响应,然后响应您的请求.

One solution is to forward the request and response using your own server. You make a call to your server, the server makes a call to lyft, waits for the response and then responds your request.

这不是仅限vue的解决方案.

这篇关于Lyft-API-从Localhost获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆