【问题标题】:"401 Unauthorized error" happened on haeroku application using github auth使用 github auth 的 haeroku 应用程序发生“401 Unauthorized error”
【发布时间】:2020-06-16 01:41:00
【问题描述】:

嗨。 我有一个 laravel 项目部署到 heroku,但是 github Auth 不顺利。 当我访问“https://my-app.herokuapp.com/login/github”时,浏览器上的heroku调试器返回错误如下:

(1/1) ClientException
Client error: `GET https://api.github.com/user` resulted in a `401 Unauthorized` response:
{
“message”: “Bad credentials”,
“documentation_url”: “https://developer.github.com/v3”
}

我用谷歌搜索了这个错误,发现每个人都说“正确设置回调 url”,但我想我已经这样做了。 这是我的代码和 GitHub 应用设置页面的屏幕截图。

web.php
Route::get('login/github', 'Auth\LoginController@ redirectToProvider');
Route::get('login/github/callback', 'Auth\LoginController@handleProviderCallback');
services.php
    ‘github' => [
        'client_id’ => env('GITHUB_CLIENT_ID'),
        'client_secret' => env('GITHUB_CLIENT_SECRET'), 
        'redirect’ => '/login/github/callback',
    ],
LoginController.php
use Illuminate\Support\Facades\Auth;
//my database access object
use App\Http\DAO;

class LoginController extends Controller
{

~~~~~~ 

    public function redirectToProvider()
    {
        return Socialite::driver('github')->redirect();
    }

    public function handleProviderCallback(Request $request)
    {
        //reading a stack trace, this line seems to occurs error.
        $github_user = Socialite::driver('github')->user();
        $github_id = [$github_user->user['login']][0];

        //if user already exists, get its User Model. Otherwise create a new Model.
        $dao = new DAO();
        $user = $dao->findUser($github_id);
        if (empty($user)) {
            $user = $dao->addNewUser($github_id);
        }

        Auth::login($user);
        return redirect('/');
    }

到底有什么问题?当然,该项目在本地主机上工作。谢谢。

【问题讨论】:

    标签: php laravel authentication oauth-2.0 github-api


    【解决方案1】:

    解决了。它需要 services.php 中的 'redirect' url 是 url 的完整路径,而不是相对路径。在本地主机上,路径“/login/github/callback”被隐式转换为“localhost/login/github/callback”,这就是我没有注意到的原因。所以正确的代码是:

        ‘github' => [
            'client_id’ => env('GITHUB_CLIENT_ID'),
            'client_secret' => env('GITHUB_CLIENT_SECRET'), 
            'redirect’ => 'https://*******.herokuapp.com/github/callback'
        ],
    

    【讨论】:

      猜你喜欢
      • 2018-02-12
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      相关资源
      最近更新 更多