首页 > 解决方案 > 尝试登录时出现“InvalidArgumentException:URI 必须是字符串或 Guzzle 中的 UriInterface”错误

问题描述

这是我这样使用时的登录功能,我获取 URI 必须是字符串错误。

当我在尝试部分的注释中使用代码时(我找不到找到此解决方案的位置),我可以正常登录,但在这种情况下,当密码错误时,catch 部分不起作用(它不会变成错误)和前端重定向到主页。

public function login(Request $request)
{
    $client = new \GuzzleHttp\Client;

    try {
        $response = $client->post(config('services.passport.login_endpoint'), [
            'form_params' => [
                'grant_type' => 'password',
                'client_id' => config('services.passport.client_id'),
                'client_secret' => config('services.passport.client_secret'),
                'username' => $request->username,
                'password' => $request->password,
            ]
        ]);
        return $response->getBody();


    /*    $username = $request->username;
        $password = $request->password;
        $request->request->add([
            'username' => $username,
            'password' => $password,
            'grant_type' => 'password',
            'client_id' => config('services.passport.client_id'),
            'client_secret' => config('services.passport.client_secret'),
            'scope' => '*'
        ]);

        $tokenRequest = $request->create(
            env('APP_URL').'/oauth/token',
            'post'
        );
        $response = Route::dispatch($tokenRequest);

        return ($response->getContent()); */

    } catch (\GuzzleHttp\Exception\BadResponseException $e) {
        if ($e->getCode() === 400) {
            return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
        } else if ($e->getCode() === 401) {
            return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
        }

        return response()->json('Something went wrong on the server.', $e->getCode());
    }
}

前端部分:

retrieveToken(context, credentials) {
  return new Promise((resolve, reject) => {
    axios.post('/login', {
      username: credentials.username,
      password: credentials.password,
    })
      .then((response) => {
        // console.log(response.data.access_token);
        const token = response.data.access_token;

        localStorage.setItem('access_token', token);
        context.commit('retrieveToken', token);
        resolve(response);
        // context.commit('addAppointment', response.data)
      })
      .catch((error) => {
        console.log(error);
        reject(error);
      });
  });
},

Services.php 文件:

<?php

 return [

/*
|--------------------------------------------------------------------------
| Third Party Services
|--------------------------------------------------------------------------
|
| This file is for storing the credentials for third party services such
| as Mailgun, Postmark, AWS and more. This file provides the de facto
| location for this type of information, allowing packages to have
| a conventional file to locate the various service credentials.
|
*/

'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
    'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
],

'postmark' => [
    'token' => env('POSTMARK_TOKEN'),
],

'ses' => [
    'key' => env('AWS_ACCESS_KEY_ID'),
    'secret' => env('AWS_SECRET_ACCESS_KEY'),
    'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],

'passport' => [
    'login_endpoint' => env('PASSPORT_LOGIN_ENDPOINT'),
    'client_id' => env('PASSPORT_CLIENT_ID'),
    'client_secret' => env('PASSPORT_CLIENT_SECRET'),
],

];

标签: laravelvue.jsguzzlelaravel-passport

解决方案


推荐阅读