{"id":23472,"date":"2024-11-21T10:38:04","date_gmt":"2024-11-21T02:38:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23472\/"},"modified":"2024-11-21T10:38:04","modified_gmt":"2024-11-21T02:38:04","slug":"laravel%e5%bc%80%e5%8f%91%ef%bc%9a%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8laravel-socialite%e5%ae%9e%e7%8e%b0%e7%a4%be%e4%ba%a4%e7%99%bb%e5%bd%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23472\/","title":{"rendered":"Laravel\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528Laravel Socialite\u5b9e\u73b0\u793e\u4ea4\u767b\u5f55\uff1f"},"content":{"rendered":"<p>\u5728\u5f53\u4eca\u65e5\u76ca\u53d1\u5c55\u7684\u793e\u4ea4\u7f51\u7edc\u65f6\u4ee3\uff0c\u793e\u4ea4\u767b\u5f55\u5df2\u6210\u4e3a\u8d8a\u6765\u8d8a\u591a\u7f51\u7ad9\u7684\u6807\u914d\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u65b9\u4fbf\u7528\u6237\u5feb\u901f\u767b\u5f55\uff0c\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u7f51\u7ad9\u589e\u52a0\u7528\u6237\u6d41\u91cf\u548c\u793e\u4ea4\u4ea4\u4e92\u3002\u867d\u7136\u81ea\u5df1\u5b9e\u73b0\u793e\u4ea4\u767b\u5f55\u4e5f\u4e0d\u96be\uff0c\u4f46\u4f7f\u7528\u73b0\u6210\u7684\u793e\u4ea4\u767b\u5f55\u7ec4\u4ef6\u53ef\u4ee5\u7701\u65f6\u7701\u529b\u3002\u5728php\u5f00\u53d1\u4e2d\uff0c socialite\u662f\u4e00\u4e2a\u6613\u4e8e\u4f7f\u7528\u7684\u793e\u4ea4\u767b\u5f55\u7ec4\u4ef6\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u6765\u770b\u770b\u5982\u4f55\u4f7f\u7528\u5b83\u5b9e\u73b0\u793e\u4ea4\u767b\u5f55\u3002<\/p>\n<p>\u4e00\u3001\u5b89\u88c5Laravel Socialite<\/p>\n<p>\u9996\u5148\u6211\u4eec\u9700\u8981\u5728 Laravel \u5e94\u7528\u4e2d\u5b89\u88c5 Socialite \u5305\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p>composer require laravel\/socialite<\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u4e4b\u540e\uff0c\u5728 config\/app.php \u6587\u4ef6\u4e2d\u6dfb\u52a0 Socialite \u670d\u52a1\u63d0\u4f9b\u5668\uff1a<\/p>\n<pre>'providers' =&amp;gt; [\n    \/\/ Other service providers...\n\n    LaravelSocialiteSocialiteServiceProvider::class,\n]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u5728 config\/app.php \u6587\u4ef6\u4e2d\u6dfb\u52a0 Socialite \u95e8\u9762\u522b\u540d\uff1a<\/p>\n<pre>'aliases' =&amp;gt; [\n    \/\/ Other aliases...\n\n    'Socialite' =&amp;gt; LaravelSocialiteFacadesSocialite::class,\n]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u6837\u6211\u4eec\u5c31\u6210\u529f\u5b89\u88c5\u4e86 Socialite \u7ec4\u4ef6\u3002<\/p>\n<p>\u4e8c\u3001\u521b\u5efa\u793e\u4ea4\u767b\u5f55\u5e94\u7528<\/p>\n<p>Socialite \u53ef\u4ee5\u652f\u6301\u8bb8\u591a\u4e0d\u540c\u7684\u793e\u4ea4\u767b\u5f55\u5e94\u7528\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u6bcf\u4e2a\u5e94\u7528\u521b\u5efa\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u3002\u4e0b\u9762\u4ee5 GitHub \u4e3a\u4f8b\u6f14\u793a\u5728 GitHub \u4e0a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 OAuth \u5e94\u7528\u3002<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u767b\u5f55\u4f60\u7684 GitHub \u8d26\u6237\uff0c\u5728\u5934\u50cf\u4e0b\u62c9\u83dc\u5355\u4e2d\u9009\u62e9 Settings-&gt;Developer settings-&gt;OAuth Apps \u8fdb\u5165 OAuth Apps \u9875\u9762\u3002<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xuyilin\/image-bed\/blog\/20211008164103.png\" class=\"aligncenter\" title=\"Laravel\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528Laravel Socialite\u5b9e\u73b0\u793e\u4ea4\u767b\u5f55\uff1f\u63d2\u56fe\" alt=\"Laravel\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528Laravel Socialite\u5b9e\u73b0\u793e\u4ea4\u767b\u5f55\uff1f\u63d2\u56fe\" \/><\/p>\n<ol>\n<li>\u70b9\u51fb New OAuth App \u6309\u94ae\uff0c\u586b\u5165\u4ee5\u4e0b\u4fe1\u606f\uff1a<\/li>\n<\/ol>\n<ul>\n<li>Application name\uff1a\u5e94\u7528\u7a0b\u5e8f\u540d\u79f0\uff1b<\/li>\n<li>Homepage URL\uff1a\u5e94\u7528\u7a0b\u5e8f\u4e3b\u9875\u7684 URL\uff1b<\/li>\n<li>Authorization callback URL\uff1a\u793e\u4ea4\u767b\u5f55\u8ba4\u8bc1\u6210\u529f\u540e\u7684\u56de\u8c03 URL\u3002<\/li>\n<\/ul>\n<p>\u586b\u5199\u5b8c\u6210\u540e\u70b9\u51fb Register application \u6309\u94ae\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xuyilin\/image-bed\/blog\/20211008164110.png\" class=\"aligncenter\" title=\"Laravel\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528Laravel Socialite\u5b9e\u73b0\u793e\u4ea4\u767b\u5f55\uff1f\u63d2\u56fe1\" alt=\"Laravel\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528Laravel Socialite\u5b9e\u73b0\u793e\u4ea4\u767b\u5f55\uff1f\u63d2\u56fe1\" \/><\/p>\n<ol>\n<li>\u751f\u6210\u5e94\u7528\u7a0b\u5e8f\u7684 Client ID \u548c Client Secret\u3002<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xuyilin\/image-bed\/blog\/20211008164118.png\" class=\"aligncenter\" title=\"Laravel\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528Laravel Socialite\u5b9e\u73b0\u793e\u4ea4\u767b\u5f55\uff1f\u63d2\u56fe2\" alt=\"Laravel\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528Laravel Socialite\u5b9e\u73b0\u793e\u4ea4\u767b\u5f55\uff1f\u63d2\u56fe2\" \/><\/p>\n<p>\u4e09\u3001\u5b9e\u73b0\u793e\u4ea4\u767b\u5f55<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u767b\u5f55\u9875\u9762\u4e2d\u6dfb\u52a0\u793e\u4ea4\u767b\u5f55\u6309\u94ae\uff0c\u5e76\u5c06\u5176\u94fe\u63a5\u5230 Socialite \u7684 GitHub \u8ba4\u8bc1\u670d\u52a1\u4e2d\uff1a<\/li>\n<\/ol>\n<pre>&lt;a href=\"%7B%7B%20url('\/auth\/github')%20%7D%7D\"&gt;\u4f7f\u7528GitHub\u767b\u5f55&lt;\/a&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u5728 Laravel \u4e2d\u521b\u5efa GitHub \u767b\u5f55\u8def\u7531\u3002\u6253\u5f00 routes\/web.php \u6587\u4ef6\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u8def\u7531\u5b9a\u4e49\uff1a<\/li>\n<\/ol>\n<pre>Route::get('auth\/github', 'AuthLoginController@redirectToProvider');\nRoute::get('auth\/github\/callback', 'AuthLoginController@handleProviderCallback');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u8981\u6c42\u6211\u4eec\u901a\u8fc7 redirectToProvider \u5c06\u7528\u6237\u91cd\u5b9a\u5411\u5230 GitHub \u8ba4\u8bc1\u670d\u52a1\uff0c\u5e76\u5728\u90a3\u91cc\u5b8c\u6210\u767b\u5f55\u8ba4\u8bc1\u3002\u5f53\u8ba4\u8bc1\u6210\u529f\u540e\uff0cGitHub \u8ba4\u8bc1\u670d\u52a1\u5c06\u91cd\u5b9a\u5411\u5230\u6307\u5b9a\u7684\u56de\u8c03 URL auth\/github\/callback\uff0c\u5e76\u901a\u8fc7\u8be5 URL \u5728 Session \u4e2d\u4f20\u9012\u8ba4\u8bc1\u6210\u529f\u7684\u7528\u6237\u4fe1\u606f\u3002<\/p>\n<ol>\n<li>\u6784\u5efa redirectToProvider \u51fd\u6570\u3002\u6253\u5f00 app\/Http\/Controllers\/Auth\/LoginController.php \u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/li>\n<\/ol>\n<pre>namespace AppHttpControllersAuth;\n\nuse IlluminateHttpRequest;\nuse AppHttpControllersController;\nuse Socialite;\n\nclass LoginController extends Controller\n{\n    \/\/ ...\n    public function redirectToProvider()\n    {\n        return Socialite::driver('github')-&amp;gt;redirect();\n    }\n    \/\/ ...\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u4eec\u8c03\u7528 Socialite \u4e2d\u7684 driver() \u65b9\u6cd5\u6765\u83b7\u53d6 GitHub \u8ba4\u8bc1\u9a71\u52a8\u7a0b\u5e8f\u7684\u5b9e\u4f8b\uff0c\u5e76\u4f7f\u7528 redirect() \u65b9\u6cd5\u5c06\u7528\u6237\u91cd\u5b9a\u5411\u5230\u767b\u5f55\u8ba4\u8bc1\u670d\u52a1\u3002<\/p>\n<ol>\n<li>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5728 handleProviderCallback \u65b9\u6cd5\u4e2d\u5904\u7406\u8ba4\u8bc1\u6210\u529f\u540e\u7684\u56de\u8c03\u3002\u6253\u5f00 app\/Http\/Controllers\/Auth\/LoginController.php \u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/li>\n<\/ol>\n<pre>\/\/ ...\npublic function handleProviderCallback()\n{\n    try {\n        $user = Socialite::driver('github')-&amp;gt;user();\n    } catch (Exception $e) {\n        return redirect('\/login');\n    }\n\n    \/\/ \u9a8c\u8bc1\u7528\u6237\u4fe1\u606f\u5e76\u5b8c\u6210\u767b\u5f55\uff0c\u4ee3\u7801\u7701\u7565\n    \/\/ ...\n}\n\/\/ ...<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u4eec\u4f7f\u7528 Socialite \u4e2d\u7684 user() \u65b9\u6cd5\u83b7\u53d6 GitHub \u8ba4\u8bc1\u670d\u52a1\u8fd4\u56de\u7684\u7528\u6237\u4fe1\u606f\u3002\u5982\u679c\u5728\u8ba4\u8bc1\u8fc7\u7a0b\u4e2d\u6709\u4efb\u4f55\u9519\u8bef\uff0c\u6211\u4eec\u5c06\u91cd\u5b9a\u5411\u7528\u6237\u5230\u5e94\u7528\u7a0b\u5e8f\u7684\u767b\u5f55\u9875\u9762\u3002\u5982\u679c\u8ba4\u8bc1\u6210\u529f\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 $user \u5bf9\u8c61\u4e2d\u7684\u4fe1\u606f\u9a8c\u8bc1\u7528\u6237\u5e76\u5b8c\u6210\u767b\u5f55\u64cd\u4f5c\u3002<\/p>\n<p>\u56db\u3001\u603b\u7ed3<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86\u4f7f\u7528 Laravel Socialite \u5b9e\u73b0 GitHub \u793e\u4ea4\u767b\u5f55\u7684\u8fc7\u7a0b\u3002\u901a\u8fc7\u7b80\u5355\u7684\u914d\u7f6e\u548c\u4e00\u4e9b\u5fc5\u8981\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u591a\u79cd\u793e\u4ea4\u767b\u5f55\u65b9\u5f0f\uff0c\u4e3a\u5e94\u7528\u7a0b\u5e8f\u589e\u52a0\u4e86\u4e00\u4e9b\u5c0f\u800c\u6709\u7528\u7684\u7279\u6027\uff0c\u5efa\u8bae\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLaravel\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528Laravel Socialite\u5b9e\u73b0\u793e\u4ea4\u767b\u5f55\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5f53\u4eca\u65e5\u76ca\u53d1\u5c55\u7684\u793e\u4ea4\u7f51\u7edc\u65f6\u4ee3\uff0c\u793e\u4ea4\u767b\u5f55\u5df2\u6210\u4e3a\u8d8a\u6765\u8d8a\u591a\u7f51\u7ad9\u7684\u6807\u914d\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u65b9\u4fbf\u7528\u6237\u5feb\u901f\u767b\u5f55\uff0c\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u7f51\u7ad9\u589e\u52a0\u7528\u6237\u6d41\u91cf\u548c\u793e\u4ea4\u4ea4\u4e92\u3002\u867d\u7136\u81ea\u5df1\u5b9e\u73b0\u793e\u4ea4\u767b\u5f55\u4e5f\u4e0d\u96be\uff0c\u4f46\u4f7f\u7528\u73b0\u6210\u7684\u793e\u4ea4\u767b\u5f55\u7ec4\u4ef6\u53ef\u4ee5\u7701\u65f6\u7701\u529b\u3002\u5728php\u5f00\u53d1\u4e2d\uff0c socialite\u662f\u4e00\u4e2a\u6613\u4e8e\u4f7f\u7528\u7684\u793e\u4ea4\u767b\u5f55\u7ec4\u4ef6\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u6765\u770b\u770b\u5982\u4f55\u4f7f\u7528\u5b83\u5b9e\u73b0\u793e\u4ea4\u767b\u5f55\u3002 \u4e00\u3001\u5b89\u88c5Laravel Socialite \u9996\u5148\u6211\u4eec\u9700\u8981\u5728 Laravel \u5e94\u7528\u4e2d\u5b89\u88c5 Socialite \u5305\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a composer require laravel\/socialite \u5b89\u88c5\u5b8c\u6210\u4e4b\u540e\uff0c\u5728 config\/app.php \u6587\u4ef6\u4e2d\u6dfb\u52a0 Socialite \u670d\u52a1\u63d0\u4f9b\u5668\uff1a &#8216;providers&#8217; =&amp;gt; [ \/\/ Other service providers&#8230; LaravelSocialiteSocialiteServiceProvider::class, ] \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\u5728 config\/app.php \u6587\u4ef6\u4e2d\u6dfb\u52a0 Socialite \u95e8\u9762\u522b\u540d\uff1a &#8216;aliases&#8217; =&amp;gt; [ \/\/ Other aliases&#8230; &#8216;Socialite&#8217; =&amp;gt; LaravelSocialiteFacadesSocialite::class, ] \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6837\u6211\u4eec\u5c31\u6210\u529f\u5b89\u88c5\u4e86 Socialite \u7ec4\u4ef6\u3002 \u4e8c\u3001\u521b\u5efa\u793e\u4ea4\u767b\u5f55\u5e94\u7528 Socialite \u53ef\u4ee5\u652f\u6301\u8bb8\u591a\u4e0d\u540c\u7684\u793e\u4ea4\u767b\u5f55\u5e94\u7528\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u6bcf\u4e2a\u5e94\u7528\u521b\u5efa\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u3002\u4e0b\u9762\u4ee5 GitHub \u4e3a\u4f8b\u6f14\u793a\u5728 GitHub \u4e0a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 OAuth \u5e94\u7528\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-23472","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=23472"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23472\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}