Skip to content

Commit

Permalink
Merge pull request #15 from MerleLiuKun/feat-revoke-token
Browse files Browse the repository at this point in the history
feat(access token): ✨ add api for revoke token
  • Loading branch information
MerleLiuKun authored Mar 4, 2024
2 parents cdfc328 + f318c90 commit 3ec3342
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: ['3.6', '3.7', '3.8', '3.9', '3.10', "3.11"]
python-version: ['3.6', '3.7', '3.8', '3.9', '3.10']
include:
- python-version: '3.10'
update-coverage: true
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ dataclasses-json = "^0.5.7"

[tool.poetry.dev-dependencies]
pytest = "^6.2.5"
pytest-cov = "^4.0.0"
responses = "^0.17.0"


[build-system]
requires = ["poetry-core"]
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
addopts = --cov=pytiktok --cov-report xml
21 changes: 21 additions & 0 deletions pytiktok/business_account_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ def refresh_access_token(
self.access_token = data["access_token"]
return data if return_json else mds.BusinessAccessToken.new_from_json_dict(data)

def revoke_access_token(self, access_token: str, return_json: bool = False) -> dict:
if not self.app_id or not self.app_secret:
raise PyTiktokError(f"Need app id and app secret.")
resp = self._request(
verb="POST",
path="tt_user/oauth2/revoke/",
json={
"client_id": self.app_id,
"client_secret": self.app_secret,
"access_token": access_token,
},
enforce_auth=False,
)

data = self.parse_response(response=resp)
return (
data
if return_json
else mds.BusinessAccessTokenRevokeResponse.new_from_json_dict(data)
)

def get_token_info(
self, access_token: str, app_id: Optional[str] = None, return_json: bool = False
) -> Union[mds.BusinessAccessTokenInfo, dict]:
Expand Down
8 changes: 8 additions & 0 deletions pytiktok/models/business_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ class BusinessAccessTokenInfo(BaseModel):
scope: Optional[str] = field(default=None)


class BusinessAccessTokenRevokeResponse(BusinessBaseResponse):
"""
Refer: https://business-api.tiktok.com/portal/docs?id=1738084387220481
"""

...


@dataclass
class BusinessAccountAudienceCountry(BaseModel):
country: Optional[str] = field(default=None)
Expand Down
Empty file.
68 changes: 68 additions & 0 deletions tests/business_account_apis/test_access_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
Tests for the access token API
"""

import pytest
import responses

from pytiktok import BusinessAccountApi, PyTiktokError


@responses.activate
def test_generate_access_token(bus_api, helpers):
with pytest.raises(PyTiktokError):
BusinessAccountApi().generate_access_token(code="test_code")

data = helpers.load_json("testsdata/business/access_token/token_resp.json")
responses.add(
responses.POST,
"https://business-api.tiktok.com/open_api/v1.3/tt_user/oauth2/token/",
json=data,
)
resp = bus_api.generate_access_token(code="test_code")
assert resp.access_token == "act.token"


@responses.activate
def test_refresh_access_token(bus_api, helpers):
with pytest.raises(PyTiktokError):
BusinessAccountApi().refresh_access_token(refresh_token="test_refresh_token")

data = helpers.load_json("testsdata/business/access_token/refresh_resp.json")
responses.add(
responses.POST,
"https://business-api.tiktok.com/open_api/v1.3/tt_user/oauth2/refresh_token/",
json=data,
)
resp = bus_api.refresh_access_token(refresh_token="test_refresh_token")
assert resp.access_token == "act.token"


@responses.activate
def test_revoke_access_token(bus_api, helpers):
with pytest.raises(PyTiktokError):
BusinessAccountApi().revoke_access_token(access_token="test_access_token")

data = helpers.load_json("testsdata/business/access_token/revoke_resp.json")
responses.add(
responses.POST,
"https://business-api.tiktok.com/open_api/v1.3/tt_user/oauth2/revoke/",
json=data,
)
resp = bus_api.revoke_access_token(access_token="test_access_token")
assert resp.code == 0


@responses.activate
def test_get_token_info(bus_api, helpers):
with pytest.raises(PyTiktokError):
BusinessAccountApi().get_token_info(access_token="test_access_token")

data = helpers.load_json("testsdata/business/access_token/token_info_resp.json")
responses.add(
responses.POST,
"https://business-api.tiktok.com/open_api/v1.3/tt_user/token_info/get/",
json=data,
)
resp = bus_api.get_token_info(access_token="test_access_token")
assert resp.app_id == "app_id"
27 changes: 27 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import json

import pytest

from pytiktok import BusinessAccountApi


class Helpers:
@staticmethod
def load_json(file_path):
with open(file_path, "rb") as f:
return json.loads(f.read().decode("utf-8"))


@pytest.fixture
def helpers():
return Helpers


@pytest.fixture
def bus_api():
return BusinessAccountApi(
app_id="test_app_id",
app_secret="test_app_secret",
access_token="test_access_token",
oauth_redirect_uri="https://example.com",
)
1 change: 1 addition & 0 deletions testsdata/business/access_token/refresh_resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code":0,"message":"OK","request_id":"20240304062229F5CE92334051531529123","data":{"access_token":"act.token","expires_in":86400,"open_id":"openid","refresh_token":"rft.token","refresh_token_expires_in":31527314,"scope":"video.publish,comment.list.manage,video.insights,video.list,user.insights,user.info.basic,user.info.username,comment.list,user.account.type,user.info.stats","token_type":"Bearer"}}
1 change: 1 addition & 0 deletions testsdata/business/access_token/revoke_resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code":0,"message":"OK","request_id":"{{request_id}}","data":{}}
1 change: 1 addition & 0 deletions testsdata/business/access_token/token_info_resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code":0,"message":"OK","request_id":"202403040625134BC8B6513D5E1A194123","data":{"app_id":"app_id","creator_id":"openid","scope":"comment.list.manage,video.list,user.insights,user.info.basic,user.account.type,video.publish,video.insights,user.info.stats,user.info.username,comment.list"}}
1 change: 1 addition & 0 deletions testsdata/business/access_token/token_resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code":0,"message":"OK","request_id":"202403040357439E497F7E6668D60F2700","data":{"access_token":"act.token","expires_in":86400,"open_id":"openid","refresh_token":"rft.token","refresh_token_expires_in":31536000,"scope":"video.publish,comment.list.manage,video.insights,video.list,user.insights,user.info.basic,user.info.username,comment.list,user.account.type,user.info.stats","token_type":"Bearer"}}

0 comments on commit 3ec3342

Please sign in to comment.