Skip to content

Commit

Permalink
added GetClientAccessTokenManagement() (*AccessTokenManagement, error)
Browse files Browse the repository at this point in the history
  • Loading branch information
Allan-Nava authored Jul 29, 2024
1 parent b62a485 commit c9757f2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tiktok/model.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package tiktok


type ErrorObject struct {
Code string `json:"code"`
Message string `json:"message"`
LogId string `json:"log_id"`
}


type AccessTokenManagement struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int64 `json:"expires_in"`
}
38 changes: 38 additions & 0 deletions tiktok/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,41 @@ var Endpoint = oauth2.Endpoint{
AuthURL: "https://www.tiktok.com/v2/auth/authorize/",
TokenURL: "https://open.tiktokapis.com/v2/oauth/token",
}

/*
# Client Access Token Management
Client access token is a type of access token that does not need user authorization. This is typically used by clients to access resources about themselves or a TikTok application, rather than to access a user's resources. The use cases are to access Research API and Commercial Content API.
curl --location --request POST 'https://open.tiktokapis.com/v2/oauth/token/' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Cache-Control: no-cache' \
--data-urlencode 'client_key=CLIENT_KEY' \
--data-urlencode 'client_secret=CLIENT_SECRET' \
--data-urlencode 'grant_type=client_credentials'
*/
func (o *tiktok) GetClientAccessTokenManagement() (*AccessTokenManagement, error) {
data := map[string]string{}
data["client_key"] = o.clientKey
data["client_secret"] = o.clientSecret
data["grant_type"] = "client_credentials"
//
resp, err := o.restyPostFormUrlEncoded(Endpoint.TokenURL, data)
if err != nil {
return nil, err
}
if resp.IsError() {
return nil, fmt.Errorf("client access token management error %s", resp.String())
}
//
var obj AccessTokenManagement
if err := json.Unmarshal(resp.Body(), &obj); err != nil {
return nil, err
}
o.debugPrint(obj)
return &obj, nil
}

}

Check failure on line 45 in tiktok/oauth2.go

View workflow job for this annotation

GitHub Actions / build (1.19.x)

syntax error: non-declaration statement outside function body

Check failure on line 45 in tiktok/oauth2.go

View workflow job for this annotation

GitHub Actions / build (1.20.x)

syntax error: non-declaration statement outside function body

Check failure on line 45 in tiktok/oauth2.go

View workflow job for this annotation

GitHub Actions / build (1.20.x)

syntax error: non-declaration statement outside function body

Check failure on line 45 in tiktok/oauth2.go

View workflow job for this annotation

GitHub Actions / build (1.21.x)

syntax error: non-declaration statement outside function body

Check failure on line 45 in tiktok/oauth2.go

View workflow job for this annotation

GitHub Actions / build (1.21.x)

syntax error: non-declaration statement outside function body

Check failure on line 45 in tiktok/oauth2.go

View workflow job for this annotation

GitHub Actions / build (1.22.x)

syntax error: non-declaration statement outside function body


13 changes: 13 additions & 0 deletions tiktok/resty.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ func (o *tiktok) restyPost(url string, body interface{}) (*resty.Response, error
return resp, nil
}

func (o *tiktok) restyPostFormUrlEncoded(url string, data map[string]string) (*resty.Response, error) {
resp, err := o.restClient.R().
SetHeader("Cache-Control", "no-cache").
SetHeader("Content-Type", "application/x-www-form-urlencoded").
SetFormData(data).
Post(url)

if err != nil {
return nil, err
}
return resp, nil
}

func (o *tiktok) restyPostWithQueryParams(url string, body interface{}, queryParams map[string]string) (*resty.Response, error) {
resp, err := o.restClient.R().
SetHeader("Accept", "application/json").
Expand Down
1 change: 1 addition & 0 deletions tiktok/tiktok.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type ITiktok interface {
HealthCheck() error
IsDebug() bool
CodeAuthUrl() string
GetClientAccessTokenManagement() (*AccessTokenManagement, error)
CreatorInfo() (*QueryCreatorInfoResponse, error)
PostVideoInit(title, videoUrl string, privacyLevel string) (*PublishVideoResponse, error)
PublishVideo(publishId string) (*PublishStatusFetchResponse, error)
Expand Down

0 comments on commit c9757f2

Please sign in to comment.