小编Sud*_*r K的帖子

如何在django rest_framework test中的APIClient标头中添加身份验证令牌

我正在用oauth2_provider我的rest_framework。我正在尝试为我的api编写测试用例。我已经获得了访问令牌。但是我无法在使用access token中验证用户使用的APIClient curl命令APIClient

curl -H "Authorization: Bearer <your_access_token>" http://localhost:8000/api/v1/users/current/
Run Code Online (Sandbox Code Playgroud)

我试过了

client.get('/api/v1/users/current/',  headers={'Authorization': 'Bearer {}'.format(self.access_token)})
Run Code Online (Sandbox Code Playgroud)

client.credentials(HTTP_AUTHORIZATION='Token ' + self.access_token)
Run Code Online (Sandbox Code Playgroud)

这是片段的一部分

from rest_framework.test import APIClient    
from rest_framework.test import APITestCase
....

class APITest(APITestCase):
    def setUp(self):
        ...
        self.client = APIClient()
        ...
        response = self.client.post('/api/v1/oauth2/token/', post_data)
        self.access_token = response.json()['access_token']

    def test_get_current_user(self):
        client.get('/api/v1/users/current/',  headers={'Authorization': 'Bearer {}'.format(self.access_token)})
Run Code Online (Sandbox Code Playgroud)

我正在回应

<HttpResponseForbidden status_code=403, "text/html; charset=utf-8">
Run Code Online (Sandbox Code Playgroud)

django django-testing oauth-provider django-unittest django-rest-framework

6
推荐指数
1
解决办法
2901
查看次数

请求权限READ_SMS后,包安装程序在Android M中崩溃

我有一个试图访问(TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);它的登录片段适用于Lollipop 5.1之前的设备.当我在Marshmallow 6.01中尝试它时,它显示了安全性异常.所以我从android docs添加了代码来请求运行时权限.这是代码

int permissionCheck = ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.READ_SMS);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.READ_SMS)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.



        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.READ_SMS},REQUEST_SMS);

            // REQUEST_SMS is an
            // app-defined int constant. …
Run Code Online (Sandbox Code Playgroud)

android android-package-managers android-permissions packageinstaller android-6.0-marshmallow

3
推荐指数
1
解决办法
4558
查看次数