-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(oauth): 增加oauth2授权相关API, 不再支持旧的token授权方式
- Loading branch information
Showing
15 changed files
with
284 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package oauth | ||
|
||
import ( | ||
"github.com/bububa/baidu-marketing/core" | ||
"github.com/bububa/baidu-marketing/model/oauth" | ||
) | ||
|
||
// AccessToken 换取授权令牌接口 | ||
func AccessToken(clt *core.SDKClient, req *oauth.AccessTokenRequest) (*oauth.AccessToken, error) { | ||
var resp oauth.AccessToken | ||
req.AppID = clt.AppID() | ||
req.SecretKey = clt.Secret() | ||
if err := clt.OAuth(req, &resp); err != nil { | ||
return nil, err | ||
} | ||
return &resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package oauth OAuth授权相关 | ||
package oauth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package oauth | ||
|
||
import ( | ||
"github.com/bububa/baidu-marketing/core" | ||
"github.com/bububa/baidu-marketing/model/oauth" | ||
) | ||
|
||
// GetUserInfo 查询授权用户信息 | ||
func GetUserInfo(clt *core.SDKClient, req *oauth.GetUserInfoRequest) (*oauth.UserInfo, error) { | ||
var resp oauth.UserInfo | ||
if err := clt.OAuth(req, &resp); err != nil { | ||
return nil, err | ||
} | ||
return &resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package oauth | ||
|
||
import ( | ||
"github.com/bububa/baidu-marketing/core" | ||
"github.com/bububa/baidu-marketing/model/oauth" | ||
) | ||
|
||
// RefreshToken 更新授权令牌接口 | ||
func RefreshToken(clt *core.SDKClient, req *oauth.RefreshTokenRequest) (*oauth.AccessToken, error) { | ||
var resp oauth.AccessToken | ||
req.AppID = clt.AppID() | ||
req.SecretKey = clt.Secret() | ||
if err := clt.OAuth(req, &resp); err != nil { | ||
return nil, err | ||
} | ||
return &resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package oauth | ||
|
||
import ( | ||
"github.com/bububa/baidu-marketing/model" | ||
"github.com/bububa/baidu-marketing/util" | ||
) | ||
|
||
// AccessToken 授权令牌 | ||
type AccessToken struct { | ||
// AccessToken 授权令牌 | ||
AccessToken string `json:"accessToken,omitempty"` | ||
// RefreshToken 刷新令牌 | ||
RefreshToken string `json:"refreshToken,omitempty"` | ||
// OpenID 获取授权用户信息标识 | ||
OpenID string `json:"openId,omitempty"` | ||
// ExpiresTime 授权令牌到期时间 | ||
ExpiresTime string `json:"expiresTime,omitempty"` | ||
// RefreshExpiresTime 更新令牌到期时间 | ||
RefreshExpiresTime string `json:"refreshExpiresTime,omitempty"` | ||
// ExpiresIn 授权令牌剩余有效时间 | ||
ExpiresIn int64 `json:"expiresIn,omitempty"` | ||
// RefreshExpiresIn 更新令牌剩余有效时间 | ||
RefreshExpiresIn int64 `json:"refreshExpiresIn,omitempty"` | ||
// UserID 授权账号 ucid | ||
UserID uint64 `json:"userId,omitempty"` | ||
} | ||
|
||
// AccessTokenRequest 换取授权令牌接口 | ||
type AccessTokenRequest struct { | ||
// AppID 应用 appid | ||
AppID string `json:"appId,omitempty"` | ||
// AuthCode 临时授权码(数据来源:通过回调接口获取) | ||
AuthCode string `json:"authCode,omitempty"` | ||
// SecretKey 应用持有的 secretKey | ||
SecretKey string `json:"secretKey,omitempty"` | ||
// GrantType 授权令牌获取模式,仅限:auth_code(授权码模式) | ||
GrantType string `json:"grantType,omitempty"` | ||
// UserID 同意授权的推广账户ID(数据来源:通过回调接口获取) | ||
UserID uint64 `json:"userId,omitempty"` | ||
} | ||
|
||
// Url implement RequestBody interface | ||
func (r AccessTokenRequest) Url() string { | ||
return util.StringsJoin(model.BASE_OAUTH_URL, "accessToken") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package oauth OAuth授权相关 | ||
package oauth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package oauth | ||
|
||
import ( | ||
"github.com/bububa/baidu-marketing/model" | ||
"github.com/bububa/baidu-marketing/util" | ||
) | ||
|
||
// GetUserInfoRequest 查询授权用户信息 | ||
type GetUserInfoRequest struct { | ||
// OpenID 授权用户查询标识 | ||
OpenID string `json:"openId,omitempty"` | ||
// AccessToken 已有的授权令牌 | ||
AccessToken string `json:"accessToken,omitempty"` | ||
// UserID 同意授权的推广账户ID | ||
UserID uint64 `json:"userId,omitempty"` | ||
// NeedSubList 是否需要子账号列表,值为true时返回subUserList | ||
NeedSubList bool `json:"needSubList,omitempty"` | ||
// PageSize 分页数量,默认100,最大不超过500 | ||
PageSize int `json:"pageSize,omitempty"` | ||
// LastPageMaxUcId 上一页返回的最大userid,用于子账号列表分页 | ||
// 查询子账号列表时,该字段为必填。第一次获取子账户列表时,该字段需要设置为1 | ||
LastPageMaxUcId uint64 `json:"lastPageMaxUcId,omitempty"` | ||
} | ||
|
||
// Url implement RequestBody interface | ||
func (r GetUserInfoRequest) Url() string { | ||
return util.StringsJoin(model.BASE_OAUTH_URL, "getUserInfo") | ||
} | ||
|
||
// UserInfo 授权用户信息 | ||
type UserInfo struct { | ||
// MasterUid 同意授权用户ucid | ||
MasterUid uint64 `json:"masterUid,omitempty"` | ||
// UserAcctType 授权账户类型 | ||
// 1: 普通账户 | ||
// 2:超管账户(客户中心和账户管家) | ||
UserAcctType int `json:"userAcctType,omitempty"` | ||
// HasNext 是否有下一页子账号列表 | ||
HasNext bool `json:"hasNext,omitempty"` | ||
// SubUserList 同意授权用户关联的子账号列表 | ||
SubUserList []SubUserInfo `json:"subUserList,omitempty"` | ||
} | ||
|
||
// SubUserInfo 同意授权用户关联的子账号 | ||
type SubUserInfo struct { | ||
// UcId 同意授权用户关联的子账号ucid | ||
UcId uint64 `json:"ucId,omitempty"` | ||
// UcName 同意授权用户关联的子账号ucname | ||
UcName string `json:"ucName,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package oauth | ||
|
||
import ( | ||
"github.com/bububa/baidu-marketing/model" | ||
"github.com/bububa/baidu-marketing/util" | ||
) | ||
|
||
// RefreshTokenRequest 更新授权令牌接口 | ||
type RefreshTokenRequest struct { | ||
// AppID 应用ID | ||
AppID string `json:"appId,omitempty"` | ||
// RefreshToken 已有的更新令牌 refreshToken | ||
RefreshToken string `json:"refreshToken,omitempty"` | ||
// SecretKey 应用密钥 | ||
SecretKey string `json:"secretKey,omitempty"` | ||
// UserID 同意授权的推广账户ID | ||
UserID uint64 `json:"userId,omitempty"` | ||
} | ||
|
||
// Url implement RequestBody interface | ||
func (r RefreshTokenRequest) Url() string { | ||
return util.StringsJoin(model.BASE_OAUTH_URL, "refreshToken") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package oauth | ||
|
||
import ( | ||
"github.com/bububa/baidu-marketing/model" | ||
"github.com/bububa/baidu-marketing/util" | ||
) | ||
|
||
// URLRequest 程序化拼接授权链接 API Request | ||
type URLRequest struct { | ||
// AppID 需要授权的应用 ID | ||
AppID string `json:"appId,omitempty"` | ||
// Scope 应用权限代码,建议从应用管理界面系统生成的授权链接中获取。 | ||
Scope string `json:"scope,omitempty"` | ||
// State 开发者自定义参数,长度限制 512 个字符,特殊字符需要 URLEncode | ||
State string `json:"state,omitempty"` | ||
// Callback 应用回调链接 | ||
Callback string `json:"callback,omitempty"` | ||
} | ||
|
||
// URL 程序化拼接授权链接 | ||
func (r URLRequest) URL() string { | ||
values := util.GetUrlValues() | ||
defer util.PutUrlValues(values) | ||
values.Set("appId", r.AppID) | ||
values.Set("scope", r.Scope) | ||
values.Set("state", r.State) | ||
values.Set("callback", r.Callback) | ||
return util.StringsJoin(model.BASE_OAUTH_URL, "page/index?", values.Encode()) | ||
} |
Oops, something went wrong.