Skip to content

Commit

Permalink
feat(oauth): 增加oauth2授权相关API, 不再支持旧的token授权方式
Browse files Browse the repository at this point in the history
  • Loading branch information
bububa committed Apr 24, 2024
1 parent 7d91e42 commit 52227d9
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 51 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
[![GitHub license](https://img.shields.io/github/license/bububa/baidu-marketing.svg)](https://github.com/bububa/baidu-marketing/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/bububa/baidu-marketing.svg)](https://github.com/bububa/baidu-marketing/releases/)

- OAuth授权 (api/oauth)
- 换取授权令牌接口 [ AccessToken(clt *core.SDKClient, req *oauth.AccessTokenRequest) (*oauth.AccessToken, error) ]
- 更新授权令牌接口 [ RefreshToken(clt *core.SDKClient, req *oauth.RefreshTokenRequest) (*oauth.AccessToken, error) ]
- 查询授权用户信息 [ GetUserInfo(clt *core.SDKClient, req *oauth.GetUserInfoRequest) (*oauth.UserInfo, error) ]
- 账户管理
- 财务管理 (api/account/balance)
- 查询账户余额成分 [ GetBalanceInfo(clt *core.SDKClient, auth model.RequestHeader, productIds[]uint64) ([]balance.BalanceInfo, error) ]
Expand Down
17 changes: 17 additions & 0 deletions api/oauth/accessToken.go
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
}
2 changes: 2 additions & 0 deletions api/oauth/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package oauth OAuth授权相关
package oauth
15 changes: 15 additions & 0 deletions api/oauth/getUserInfo.go
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
}
17 changes: 17 additions & 0 deletions api/oauth/refreshToken.go
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
}
74 changes: 36 additions & 38 deletions core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,14 @@ func defaultHttpClient() *http.Client {
// SDKClient object
type SDKClient struct {
httpClient *http.Client
token string
ocpcToken string
username string
password string
appID string
secret string
debug bool
}

// NewSDKClient init sdk client
func NewSDKClient(token string, ocpcToken string) *SDKClient {
func NewSDKClient(appID string, secret string) *SDKClient {
return &SDKClient{
token: token,
ocpcToken: ocpcToken,
httpClient: defaultHttpClient(),
}
}
Expand All @@ -54,40 +50,21 @@ func (c *SDKClient) SetHttpClient(httpClient *http.Client) {
c.httpClient = httpClient
}

// Token get token
func (c SDKClient) Token() string {
return c.token
}

// OcpcToken get ocpc token
func (c SDKClient) OcpcToken() string {
return c.ocpcToken
// SetDebug set debug mode
func (c *SDKClient) SetDebug(debug bool) {
c.debug = debug
}

// SetUser set username password
func (c *SDKClient) SetUser(username string, password string) {
c.username = username
c.password = password
func (c *SDKClient) AppID() string {
return c.appID
}

// SetDebug set debug mode
func (c *SDKClient) SetDebug(debug bool) {
c.debug = debug
func (c *SDKClient) Secret() string {
return c.secret
}

// Do execute api request
func (c *SDKClient) Do(req *model.Request, resp interface{}) (*model.ResponseHeader, error) {
if req.Header.Token == "" {
req.Header.Token = c.token
}
if req.Header.AccessToken == "" {
if req.Header.Username == "" {
req.Header.Username = c.username
}
if req.Header.Password == "" {
req.Header.Password = c.password
}
}
buf := util.GetBufferPool()
defer util.PutBufferPool(buf)
if err := json.NewEncoder(buf).Encode(req); err != nil {
Expand All @@ -107,7 +84,7 @@ func (c *SDKClient) Do(req *model.Request, resp interface{}) (*model.ResponseHea
}
return &reqResp.Header, reqResp
}
if resp != nil {
if resp != nil && reqResp.Body != nil {
err = json.Unmarshal(reqResp.Body, resp)
if err != nil {
return &reqResp.Header, err
Expand All @@ -117,9 +94,6 @@ func (c *SDKClient) Do(req *model.Request, resp interface{}) (*model.ResponseHea
}

func (c *SDKClient) Conversion(req model.ConversionRequest, resp interface{}) (*model.ResponseHeader, error) {
if req.OcpcToken() == "" {
req.SetOcpcToken(c.ocpcToken)
}
buf := util.GetBufferPool()
defer util.PutBufferPool(buf)
if err := json.NewEncoder(buf).Encode(req); err != nil {
Expand All @@ -139,7 +113,7 @@ func (c *SDKClient) Conversion(req model.ConversionRequest, resp interface{}) (*
}
return &reqResp.Header, reqResp
}
if resp != nil {
if resp != nil && reqResp.Body != nil {
err = json.Unmarshal(reqResp.Body, resp)
if err != nil {
return &reqResp.Header, err
Expand All @@ -164,6 +138,30 @@ func (c *SDKClient) ActionCb(req model.ActionCbRequest) error {
return nil
}

// OAuth execute oauth api request
func (c *SDKClient) OAuth(req model.RequestBody, resp interface{}) error {
buf := util.GetBufferPool()
defer util.PutBufferPool(buf)
if err := json.NewEncoder(buf).Encode(req); err != nil {
return err
}
var reqResp model.DataResponse
err := c.Post(req.Url(), buf.Bytes(), &reqResp)
if err != nil {
return err
}
if reqResp.IsError() {
return reqResp
}
if resp != nil && reqResp.Data != nil {
err = json.Unmarshal(reqResp.Data, resp)
if err != nil {
return err
}
}
return nil
}

// Post data through api
func (c *SDKClient) Post(reqUrl string, bs []byte, resp interface{}) error {
debug.PrintPostJSONRequest(reqUrl, bs, c.debug)
Expand Down
1 change: 1 addition & 0 deletions model/account/getUserListByMccid.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
// GetUserListByMccidRequest 账户管家子账号 API Request
type GetUserListByMccidRequest struct{}

// Url implement RequestBody interface
func (r GetUserListByMccidRequest) Url() string {
return util.StringsJoin(model.BASE_URL_FEED, "MccFeedService/getUserListByMccid")
}
Expand Down
7 changes: 7 additions & 0 deletions model/const.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package model

const (
// BASE_OAUTH_URL oauth api base url
BASE_OAUTH_URL = "https://u.baidu.com/oauth/"
// BASE_URL_SMS sms/service api base url
BASE_URL_SMS = "https://api.baidu.com/json/sms/service/" // api base url
// BASE_URL_FEED feed/v2 api base url
Expand All @@ -10,3 +12,8 @@ const (
// ACTIONCB_URL
ACTIONCB_URL = "https://als.baidu.com/cb/actionCb"
)

const (
// PlatformID
PlatformID = "4960345965958561794"
)
45 changes: 45 additions & 0 deletions model/oauth/accessToken.go
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")
}
2 changes: 2 additions & 0 deletions model/oauth/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package oauth OAuth授权相关
package oauth
50 changes: 50 additions & 0 deletions model/oauth/getUserInfo.go
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"`
}
23 changes: 23 additions & 0 deletions model/oauth/refreshToken.go
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")
}
29 changes: 29 additions & 0 deletions model/oauth/url.go
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())
}
Loading

0 comments on commit 52227d9

Please sign in to comment.