简体   繁体   English

Laravel API cURL对Python的请求

[英]Laravel API cURL Request to Python

I followed this Laravel token API tutorial: http://rjv.im/post/95988160186/api-token-authentication-with-laravel-and-sentry-part . 我遵循了Laravel令牌API教程: http ://rjv.im/post/95988160186/api-token-authentication-with-laravel-and-sentry-part。 I have written the following cURL request to communicate with my API: 我已编写以下cURL请求以与我的API通信:

curl -H "X-Auth-Token:tokenhere" http://localhost:8000/account

The request works properly, and accurately returns the expected data. 该请求正常工作,并准确返回预期数据。 When I translate this to Python I receive urllib2.HTTPError: HTTP Error 401: Unauthorized 当我将其翻译成Python时,我收到urllib2.HTTPError: HTTP Error 401: Unauthorized

import urllib2
req = urllib2.Request('http://localhost:8000/account')
req.add_header("X-Auth-Token", "tokenhere")
resp = urllib2.urlopen(req)
content = resp.read()
print content

If I pass user credentials using basic auth instead of an X-Auth-Token, the request works as expected: 如果我使用基本身份验证而不是X-Auth-Token传递用户凭据,则请求将按预期工作:

import urllib2

def basic_authorization(user, password):
    s = user + ":" + password
    return "Basic " + s.encode("base64").rstrip()

req = urllib2.Request("http://localhost:8000/account", headers = { "Authorization": basic_authorization("usernameHere", "passwordHere"), })

f = urllib2.urlopen(req)

print f.read()

Any assistance would be much appreciated. 任何帮助将不胜感激。

There is something you missed in the tutorial. 您在本教程中错过了一些东西。 In the tokens table there is a column: 在令牌表中有一列:

$table->string('client');

It is important from which client you are sending your request. 从哪个客户端发送请求很重要。 I am using https://github.com/hisorange/browser-detect to detect from which client I got the request. 我正在使用https://github.com/hisorange/browser-detect来检测我从哪个客户端收到了请求。

But for now I will just try to see User Agent. 但是现在,我将仅尝试查看用户代理。 In my laravel code I just logged every request to see what's happening with the following code: 在我的laravel代码中,我刚刚记录了每个请求,以查看以下代码的情况:

Route::filter('auth.token', function($route, $request)
{
....

  Log::info($request);

....
}

Now, Let's see: 现在,让我们看看:

When I use curl from command line: 当我从命令行使用curl时:

curl -u user@example.com:password -X GET http://localhost:8000/account

My User Agent is 我的用户代理是

User-Agent:   curl/7.32.0

I sent the same from python using your code above, User Agent is: 我使用上面的代码从python发送了同样的内容,用户代理为:

User-Agent:      Python-urllib/2.7

Ah! 啊! That must be it. 一定是这样。 You have to authenticate your user at least once using Basic Auth, it will give you a token and that token is valid for only that client. 您必须使用基本身份验证至少对用户进行一次身份验证,这将为您提供一个令牌,并且该令牌仅对该客户端有效。 In the first part http://rjv.im/post/78940780589/api-token-authentication-with-laravel-and-sentry-part of tutorial there was no such condition. 在本教程的第一部分http://rjv.im/post/78940780589/api-token-authentication-with-laravel-and-sentry-part中 ,没有这种情况。 In the comments I received someone posted a query on how to support multiple clients, so this example was made to solve that problem. 在评论中,我收到某人发布了有关如何支持多个客户端的查询,因此创建了此示例来解决该问题。

Apart from that, may I suggest this library: https://github.com/chrisbjr/api-guard It supports Rate Limiting, easy to integrate with Sentry. 除此之外,我是否可以建议该库: https : //github.com/chrisbjr/api-guard它支持速率限制,易于与Sentry集成。 It's a bit different from my tutorial. 与我的教程有些不同。 Using my solution you can hit any endpoint using Basic Auth or Token. 使用我的解决方案,您可以使用基本身份验证或令牌访问任何端点。 Using above library, only token is permitted, so there is dedicated route to generate token. 使用上述库,仅允许令牌,因此有专用的路径来生成令牌。 Let me know how it goes. 让我知道事情的后续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM