Skip to content
This repository has been archived by the owner on Oct 8, 2023. It is now read-only.

Commit

Permalink
Use typing.Tuple instead to solve below Python 3.9 compatibility issue
Browse files Browse the repository at this point in the history
Add since_date/until_date on get_page_default_web_insight
Change some fields in responses to Optional to prevent exceptions.
Add more type annotations
Update README.md
Add missing .env path setting
Add Literal for get_page_default_web_insight's period paramter
Auto renew long-lived page token & use TinyDB to store
Update README
Fix compose_fb_graph_api_page_request
Add error handle if an access_token does not have any page scope related
Check if InsightsResponse has valid data
Throw exception in get_long_lived_token if fb_app_id or fb_app_secret is invalid
Check if the possible return error in get_page_token_from_user_token
Check if since is less than until parameters
Update README.md
Update README.md
Update README.md
docs(README): add usage examples
build(.env): remove credentials
  • Loading branch information
grimmer0125 authored and david30907d committed Sep 30, 2023
1 parent f5ae980 commit b6c8396
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 92 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ fb_app_id=
fb_app_secret=
fb_default_page_id=
fb_default_page_access_token=
GOOGLE_APPLICATION_CREDENTIALS=
BIGQUERY_PROJECT=
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# Python Facebook Page Insights Client
# Python Page Insights Client

Currently, it is used by https://github.com/pycontw/pycon-etl . Check [dags/ods/fb_page_insights/udfs/fb_page_insights_data_uploader.py](https://github.com/pycontw/pycon-etl/blob/master/dags/ods/fb_page_insights/dags/fb_page_insights_2_bigquery.py) as an example of how to use this Python module.

## Usage

## Get needed secrets first
### Get needed secrets first

https://github.com/facebook/facebook-python-business-sdk#register-an-app is a reference and the steps are
1. create a FB app and get its `app_id` and `secret`,
2. In terms of `user_access_token`, make sure you are a registered developer of this fb app and get user access token on Graph Explorer. You will get a short-term user_token by default, expired in 2 or 3 months. To get long-term token, choose either of the below ways
- using Graph Exploer -> Access token tool -> Extend access token
- invoke get_long_lived_user_token of this library
2. In terms of `user_access_token/page_access_token`, make sure you are a registered developer of this fb app, and get user_access_token or page_user_token on Graph Explorer with selected scopes, read_insights & pages_read_engagement. Either user_token or page_token is working. You will get a short-term token by default, expired in 2 or 3 months. To get long-term token, choose either of the below ways
- this library will automatically get the long-lived token and cache it in local db.json
- manually use Graph Exploer -> Access token tool -> Extend access token to get long-lived token first.

Rather than Graph Explorer, https://github.com/pycontw/python-fb-page-insights-client/issues/6 introduces another way which does not to be a registered developer of this fb app. But this way is not recommanded.
Rather than Graph Explorer, https://github.com/pycontw/python-fb-page-insights-client/issues/6 introduces another way which does not to be a registered developer of this fb app. But this way is not recommended.

### Pass secrets

Expand All @@ -25,11 +27,13 @@ fb_default_page_access_token=
```

You can choose any of below ways:
- pass them as function paramets
- manually export them as enviornments variables
- pass them as function parameters
- manually export them as environment variables
- create a .env to include them

if fb_user_access_token is filled, fb_default_page_access_token is not necessary and will be ignored. fb_user_access_token will be used to get page token internally.
if fb_default_page_access_token is filled and valid, fb_user_access_token usage will be skipped. fb_user_access_token is used to get page token internally. So you only need to fill either a valid fb_default_page_access_token or fb_user_access_token.

You can skip fb_app_id and fb_app_secret if you are sure if fb_user_access_token/fb_default_page_access_token is long-lived token. In https://developers.facebook.com/tools/debug/accesstoken/, You can paste the token to check its "Expires" and "Valid" field.

## Fetch data

Expand All @@ -44,10 +48,6 @@ Use `FBPageInsight` class to fetch. Please checkout the unit test code as an exa
- `Calls within one hour = 4800 * Number of Engaged Users`
- api response header inclues `x-business-use-case-usage`

### Somehow FB will return invalid data sometimes

FB API is not very stable and plrease try again.

## Development

1. `poetry shell`
Expand All @@ -66,4 +66,5 @@ Two methods:

## TODO:

https://github.com/pycontw/python-fb-page-insights-client/discussions/4
https://github.com/pycontw/facebook_page_insights_client/discussions/4

14 changes: 13 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ python = "^3.7.1"
pydantic = "^1.8.2"
requests = "^2.25.1"
python-dotenv = "^0.18.0"
tinydb = "^4.5.1"

[tool.poetry.dev-dependencies]
autopep8 = "^1.5.7"
Expand Down
Loading

0 comments on commit b6c8396

Please sign in to comment.