laravel5.6 QQ 第三方登录

举报
lxw1844912514 发表于 2022/03/28 00:19:03 2022/03/28
【摘要】 https://socialiteproviders.github.io/providers/qq.html 1. Installation // This assumes that you have composer installed globallycomposer require socialiteprov...

https://socialiteproviders.github.io/providers/qq.html

1. Installation


    
  1. // This assumes that you have composer installed globally
  2. composer require socialiteproviders/qq

# 2. Service Provider

  • Remove Laravel\Socialite\SocialiteServiceProvider from your providers[] array in config\app.php if you have added it already.

  • Add \SocialiteProviders\Manager\ServiceProvider::class to your providers[] array in config\app.php.

For example:


    
  1. 'providers' => [
  2. // a whole bunch of providers
  3. // remove 'Laravel\Socialite\SocialiteServiceProvider',
  4. \SocialiteProviders\Manager\ServiceProvider::class, // add
  5. ];

 

  • Note: If you would like to use the Socialite Facade, you need to install it.

# 3. Event Listener

  • Add SocialiteProviders\Manager\SocialiteWasCalled event to your listen[] array in app/Providers/EventServiceProvider.

  • Add your listeners (i.e. the ones from the providers) to the SocialiteProviders\Manager\SocialiteWasCalled[] that you just created.

  • The listener that you add for this provider is 'SocialiteProviders\\QQ\\QqExtendSocialite@handle',.

  • Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers.

For example:


    
  1. /**
  2. * The event handler mappings for the application.
  3. *
  4. * @var array
  5. */
  6. protected $listen = [
  7. \SocialiteProviders\Manager\SocialiteWasCalled::class => [
  8. // add your listeners (aka providers) here
  9. 'SocialiteProviders\\QQ\\QqExtendSocialite@handle',
  10. ],
  11. ];

 

# Reference

# 4. Configuration setup

You will need to add an entry to the services configuration file so that after config files are cached for usage in production environment (Laravel command artisan config:cache) all config is still available.

# Add to config/services.php.


    
  1. 'qq' => [
  2. 'client_id' => env('QQ_KEY'),
  3. 'client_secret' => env('QQ_SECRET'),
  4. 'redirect' => env('QQ_REDIRECT_URI')
  5. ],

# 5. Usage

  • Laravel docs on configuration

  • You should now be able to use it like you would regularly use Socialite (assuming you have the facade installed):

return Socialite::with('QQ')->redirect();

   

 

# Lumen Support

You can use Socialite providers with Lumen. Just make sure that you have facade support turned on and that you follow the setup directions properly.

Note: If you are using this with Lumen, all providers will automatically be stateless since Lumen does not keep track of state.

Also, configs cannot be parsed from the services[] in Lumen. You can only set the values in the .env file as shown exactly in this document. If needed, you can also override a config (shown below).

# Stateless

  • You can set whether or not you want to use the provider as stateless. Remember that the OAuth provider (Twitter, Tumblr, etc) must support whatever option you choose.

Note: If you are using this with Lumen, all providers will automatically be stateless since Lumen does not keep track of state.


    
  1. // to turn off stateless
  2. return Socialite::with('QQ')->stateless(false)->redirect();
  3. // to use stateless
  4. return Socialite::with('QQ')->stateless()->redirect();

# Overriding a config

If you need to override the provider's environment or config variables dynamically anywhere in your application, you may use the following:


    
  1. $clientId = "secret";
  2. $clientSecret = "secret";
  3. $redirectUrl = "http://yourdomain.com/api/redirect";
  4. $additionalProviderConfig = ['site' => 'meta.stackoverflow.com'];
  5. $config = new \SocialiteProviders\Manager\Config($clientId, $clientSecret, $redirectUrl, $additionalProviderConfig);
  6. return Socialite::with('QQ')->setConfig($config)->redirect();

# Retrieving the Access Token Response Body

Laravel Socialite by default only allows access to the access_token. Which can be accessed via the \Laravel\Socialite\User->token public property. Sometimes you need access to the whole response body which may contain items such as a refresh_token.

You can get the access token response body, after you called the user() method in Socialite, by accessing the property $user->accessTokenResponseBody;


    
  1. $user = Socialite::driver('QQ')->user();
  2. $accessTokenResponseBody = $user->accessTokenResponseBody;

# Reference

参考地址:https://laravel-china.org/docs/laravel/5.6/socialite/1418

https://socialiteproviders.github.io/providers/qq.html

文章来源: blog.csdn.net,作者:lxw1844912514,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/lxw1844912514/article/details/100027726

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。