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

Add more metric for low level API usage #11

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
95836ea
Use typing.Tuple instead to solve below Python 3.9 compatibility issue
grimmer0125 Aug 27, 2021
8d0a965
Add since_date/until_date on get_page_default_web_insight
grimmer0125 Aug 28, 2021
fa3caac
Add more type annotations
grimmer0125 Aug 29, 2021
bad7bea
Update README.md
grimmer0125 Sep 2, 2021
4a25b21
Add missing .env path setting
grimmer0125 Sep 3, 2021
f858903
Add Literal for get_page_default_web_insight's period paramter
grimmer0125 Sep 5, 2021
4b238f6
Auto renew long-lived page token & use TinyDB to store
grimmer0125 Sep 22, 2021
de2f321
Update README
grimmer0125 Oct 4, 2021
794a407
Fix compose_fb_graph_api_page_request
grimmer0125 Oct 4, 2021
bd221ff
Merge pull request #8 from pycontw/auto_get_cache_long_lived_page_token
grimmer0125 Oct 4, 2021
e9a1d93
Add error handle if an access_token does not have any page scope related
grimmer0125 Oct 5, 2021
7d046f4
Check if InsightsResponse has valid data
grimmer0125 Oct 6, 2021
34d4a38
Throw exception in get_long_lived_token if fb_app_id or fb_app_secret…
grimmer0125 Oct 6, 2021
611cf46
Merge pull request #9 from pycontw/error_handle
grimmer0125 Oct 6, 2021
00c59d3
Check if the possible return error in get_page_token_from_user_token
grimmer0125 Oct 6, 2021
934dd74
Check if since is less than until parameters
grimmer0125 Oct 6, 2021
26b75c4
Merge pull request #10 from pycontw/check_since_until_range
grimmer0125 Oct 6, 2021
407e66e
Update README.md
grimmer0125 Oct 6, 2021
8fc4606
PostDetailMetric is depreciated & use PostMetric instead since mergin…
grimmer0125 Oct 6, 2021
f8e5b83
Format the code via black
grimmer0125 Oct 7, 2021
f1858f0
Add more page and (page) post metrics (WIP)
grimmer0125 Oct 7, 2021
785532a
Add PageStoryValue and PostReactionType, PageNegativeFeedback, PagePo…
grimmer0125 Oct 28, 2021
7778a71
Use @root_validator and name field to distinguish InsightsValue
grimmer0125 Dec 6, 2021
75ef3a9
Refactor compose_fb_graph_api_page_request
grimmer0125 Dec 6, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
fb_user_access_token=
fb_app_id=
fb_app_secret=
fb_default_page_id=
fb_user_access_token=EAAPzLwL9ZA20BAFf9SxcVMwt6q0PKQQjpDv7Tny6T4gFVepZBZCGhgUTEqe5m1Y8Ial1fQFVqCyRpuoeD2pQTRP2gJg24BHzN3qOqnsNAZBgDYUBFkkw4ZAufpHme1kSjAiasYBv2LHSIeDF3wHZCvqprc7nkK4rW0XeZCPZBLACTHGySZCZADMeND3KumZCuoeZB7QZD
fb_app_id=1111808169305965
fb_app_secret=72d5258eaec14dbf0e09dff5b961b2db
fb_default_page_id=160712400714277
fb_default_page_access_token=
GOOGLE_APPLICATION_CREDENTIALS=./service-account.json
BIGQUERY_PROJECT=pycontw-225217
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# Python Facebook Page Insights Client

Currently, it is used by https://github.com/pycontw/pycon-etl

## Usage

## 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 @@ -67,3 +67,4 @@ Two methods:
## TODO:

https://github.com/pycontw/python-fb-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