Skip to content

Commit

Permalink
toutiao&xigua based on douyin provider (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNorthMemory authored Jul 21, 2022
1 parent 45f4931 commit 0913457
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ This tool now supports platforms such as Facebook, GitHub, Google, Figma, Linked
- [Alipay](#alipay)
- [DingTalk](#dingtalk)
- [Douyin](#douyin)
- [TouTiao](#toutiao)
- [XiGua](#xigua)
- [Baidu](#baidu)
- [Feishu](#feishu)
- [Taobao](#taobao)
Expand Down Expand Up @@ -312,6 +314,44 @@ $user = $socialite->create('douyin')->userFromCode('here is auth code');
$user = $socialite->create('douyin')->withOpenId('openId')->userFromToken('here is the access token');
```

### [TouTiao](https://open.douyin.com/platform/resource/docs/develop/permission/toutiao-or-xigua/OAuth2.0/)

> Note: using the `toutiao` create that if you get user information directly using access token, set up the openid first. the openid can be obtained by code when access is obtained, so call `userFromCode()` automatically configured for you openid, if call `userFromToken()` first call `withOpenId()`
```php
$config = [
'toutiao' => [
'client_id' => 'your app id',
'client_secret' => 'your app secret',
'redirect' => 'redirect URL'
]
];

$socialite = new SocialiteManager($config);

$user = $socialite->create('toutiao')->userFromCode('here is auth code');
$user = $socialite->create('toutiao')->withOpenId('openId')->userFromToken('here is the access token');
```

### [XiGua](https://open.douyin.com/platform/resource/docs/develop/permission/toutiao-or-xigua/OAuth2.0/)

> Note: using the `xigua` create that if you get user information directly using access token, set up the openid first. the openid can be obtained by code when access is obtained, so call `userFromCode()` automatically configured for you openid, if call `userFromToken()` first call `withOpenId()`
```php
$config = [
'xigua' => [
'client_id' => 'your app id',
'client_secret' => 'your app secret',
'redirect' => 'redirect URL'
]
];

$socialite = new SocialiteManager($config);

$user = $socialite->create('xigua')->userFromCode('here is auth code');
$user = $socialite->create('xigua')->withOpenId('openId')->userFromToken('here is the access token');
```


### [Baidu](https://developer.baidu.com/wiki/index.php?title=docs/oauth)

Expand Down
40 changes: 40 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Socialite 是一个 [OAuth2](https://oauth.net/2/) 认证工具。 它的灵感
- [支付宝](#支付宝)
- [钉钉](#钉钉)
- [抖音](#抖音)
- [头条](#头条)
- [西瓜](#西瓜)
- [百度](#百度)
- [飞书](#飞书)
- [淘宝](#淘宝)
Expand Down Expand Up @@ -324,6 +326,44 @@ $user = $socialite->create('douyin')->userFromCode('here is auth code');
$user = $socialite->create('douyin')->withOpenId('openId')->userFromToken('here is the access token');
```

### [头条](https://open.douyin.com/platform/resource/docs/develop/permission/toutiao-or-xigua/OAuth2.0/)

> 注意: 使用`头条`服务提供的时候,如果你想直接使用 access_token 获取用户信息时,请先设置 openid。 先调用 `withOpenId()` 再调用 `userFromToken()`
```php
$config = [
'toutiao' => [
'client_id' => 'your app id',
'client_secret' => 'your app secret',
'redirect' => 'redirect URL'
]
];

$socialite = new SocialiteManager($config);

$user = $socialite->create('toutiao')->userFromCode('here is auth code');
$user = $socialite->create('toutiao')->withOpenId('openId')->userFromToken('here is the access token');
```

### [西瓜](https://open.douyin.com/platform/resource/docs/develop/permission/toutiao-or-xigua/OAuth2.0/)

> 注意: 使用`西瓜`服务提供的时候,如果你想直接使用 access_token 获取用户信息时,请先设置 openid。 先调用 `withOpenId()` 再调用 `userFromToken()`
```php
$config = [
'xigua' => [
'client_id' => 'your app id',
'client_secret' => 'your app secret',
'redirect' => 'redirect URL'
]
];

$socialite = new SocialiteManager($config);

$user = $socialite->create('xigua')->userFromCode('here is auth code');
$user = $socialite->create('xigua')->withOpenId('openId')->userFromToken('here is the access token');
```


### [百度](https://developer.baidu.com/wiki/index.php?title=docs/oauth)

Expand Down
33 changes: 33 additions & 0 deletions src/Providers/TouTiao.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Overtrue\Socialite\Providers;

use JetBrains\PhpStorm\Pure;
use Overtrue\Socialite\Contracts;
use Overtrue\Socialite\User;

/**
* @see https://open.douyin.com/platform/resource/docs/openapi/account-permission/toutiao-get-permission-code
*/
class TouTiao extends DouYin
{
public const NAME = 'toutiao';

protected string $baseUrl = 'https://open.snssdk.com';

protected function getAuthUrl(): string
{
return $this->buildAuthUrlFromBase($this->baseUrl . '/oauth/authorize/');
}

#[Pure]
protected function mapUserToObject(array $user): Contracts\UserInterface
{
return new User([
Contracts\ABNF_ID => $user[Contracts\ABNF_OPEN_ID] ?? null,
Contracts\ABNF_NAME => $user[Contracts\ABNF_NICKNAME] ?? null,
Contracts\ABNF_NICKNAME => $user[Contracts\ABNF_NICKNAME] ?? null,
Contracts\ABNF_AVATAR => $user[Contracts\ABNF_AVATAR] ?? null,
]);
}
}
33 changes: 33 additions & 0 deletions src/Providers/XiGua.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Overtrue\Socialite\Providers;

use JetBrains\PhpStorm\Pure;
use Overtrue\Socialite\Contracts;
use Overtrue\Socialite\User;

/**
* @see https://open.douyin.com/platform/resource/docs/openapi/account-permission/xigua-get-permission-code
*/
class XiGua extends DouYin
{
public const NAME = 'xigua';

protected string $baseUrl = 'https://open-api.ixigua.com';

protected function getAuthUrl(): string
{
return $this->buildAuthUrlFromBase($this->baseUrl . '/oauth/connect');
}

#[Pure]
protected function mapUserToObject(array $user): Contracts\UserInterface
{
return new User([
Contracts\ABNF_ID => $user[Contracts\ABNF_OPEN_ID] ?? null,
Contracts\ABNF_NAME => $user[Contracts\ABNF_NICKNAME] ?? null,
Contracts\ABNF_NICKNAME => $user[Contracts\ABNF_NICKNAME] ?? null,
Contracts\ABNF_AVATAR => $user[Contracts\ABNF_AVATAR] ?? null,
]);
}
}
2 changes: 2 additions & 0 deletions src/SocialiteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ class SocialiteManager implements Contracts\FactoryInterface
Providers\QQ::NAME => Providers\QQ::class,
Providers\Taobao::NAME => Providers\Taobao::class,
Providers\Tapd::NAME => Providers\Tapd::class,
Providers\TouTiao::NAME => Providers\TouTiao::class,
Providers\WeChat::NAME => Providers\WeChat::class,
Providers\WeWork::NAME => Providers\WeWork::class,
Providers\Weibo::NAME => Providers\Weibo::class,
Providers\XiGua::NAME => Providers\XiGua::class,
];

#[Pure]
Expand Down

0 comments on commit 0913457

Please sign in to comment.