【发布时间】: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