Android Studio给予错误401与Laravel登录API

ovfsdjhp  于 2023-01-21  发布在  Android
关注(0)|答案(1)|浏览(140)

我有一个简单的问题,我写的API在laravel,我会把它放在我的android应用程序,但虽然注册部分和注销部分工作正常,当我检查从postman在登录部分,它采取令牌和登录,但当我们来到应用程序,我得到一个401错误时,登录,我想知道为什么?我的登录代码是在控制器如下:

$credentials = $request->validate([
‘email’=>‘required|email’,
‘password’=>‘required’
]);
if(Auth::attempt($credentials)){
$user=Auth::user();
$token=md5(time()).‘.’.md5($request->email);
$user->forceFill([
‘api_token’=>$token,
])->save();
return response()->json([
‘token’=>$token
]);

   }
   return response()->json(['message'=>'The provided credentials do not match our records'],401);
}

它在Postman中给出正确的,它不会引起任何问题,但是我在应用程序中注册了。这不是问题。它在登录时给出401,我想知道为什么?我在应用程序中的代码如下:

private void sendLogin() {
JSONObject params= new JSONObject();
try {
params.put(“email”,email);
params.put(“password”,password);

    }catch (JSONException e){
        e.printStackTrace();
    }
    String data = params.toString();
    String url = getString(R.string.api_server)+"/login";
    new Thread(new Runnable() {
        @Override
        public void run() {
            Http http = new Http(LoginActivity.this,url);
            http.setMethod("POST");
            http.setData(data);
            http.send();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Integer code = http.getStatusCode();
                    if(code == 200){
                        try{
                            JSONObject response = new JSONObject(http.getResponse());
                            String token = response.getString("token");
                            localStorage.setToken(token);
                            Intent i = new Intent(LoginActivity.this,UserActivity.class);
                            startActivity(i);
                            finish();
                        }catch (JSONException e){
                            e.printStackTrace();
                        }
                    }
                    else if(code ==422){
                        try{
                           JSONObject response = new JSONObject(http.getResponse());
                           String msg = response.getString("message");
                           alertFail(msg);
                        }catch (JSONException e){
                            e.printStackTrace();
                        }
                    }
                    else if(code == 401){
                        try{
                            JSONObject response = new JSONObject(http.getResponse());
                            String msg = response.getString("message");
                            alertFail(msg);
                        }catch (JSONException e){
                            e.printStackTrace();
                        }
                    }
                    else{
                        Toast.makeText(LoginActivity.this,"Error"+code,Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }).start();
}

如果你能帮我我会很高兴的

a14dhokn

a14dhokn1#

用标题发送

["Accept"] = "application/json";
["Content-Type"] = "application/json";
["withCredentials"] = true;

并在env laravel中添加此(值仅为示例)

SESSION_DOMAIN=.localhost
SANCTUM_STATEFUL_DOMAINS=localhost,127.0.0.1

相关问题