-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #270 from MerleLiuKun/feat-threads
Feat threads
- Loading branch information
Showing
8 changed files
with
237 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
## Introduction | ||
|
||
You may use the Threads API to enable people to create and publish content on a person’s behalf on Threads, and to | ||
display those posts within your app solely to the person who created it. | ||
|
||
## How to use | ||
|
||
Just like the base `Graph API`. | ||
|
||
The following code snippet shows how to perform an OAuth flow with the Threads API: | ||
|
||
```python | ||
from pyfacebook import ThreadsGraphAPI | ||
|
||
api = ThreadsGraphAPI( | ||
app_id="Your app id", | ||
app_secret="Your app secret", | ||
oauth_flow=True, | ||
redirect_uri="Your callback domain", | ||
scope=["threads_basic", "threads_content_publish", "threads_read_replies", "threads_manage_replies", | ||
"threads_manage_insights"] | ||
) | ||
|
||
# Got authorization url | ||
api.get_authorization_url() | ||
# https://threads.net/oauth/authorize?response_type=code&client_id=app_id&redirect_uri=https%3A%2F%2Fexample.com%2Fcallback&scope=threads_basic%2Cthreads_content_publish%2Cthreads_read_replies%2Cthreads_manage_replies%2Cthreads_manage_insights&state=PyFacebook | ||
|
||
# Once the user has authorized your app, you will get the redirected URL. | ||
# like `https://example.com/callback?code=AQBZzYhLZB&state=PyFacebook#_` | ||
token = api.exchange_user_access_token(response="Your response url") | ||
print(token) | ||
# {'access_token': 'access_token', 'user_id': 12342412} | ||
``` | ||
|
||
After those steps, you can use the `api` object to call the Threads API. | ||
|
||
For example: | ||
|
||
```python | ||
api.get_object(object_id="me", fields=["id"]) | ||
|
||
# {'id': '12342412'} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .graph import GraphAPI, BasicDisplayAPI, ServerSentEventAPI | ||
from .graph import GraphAPI, BasicDisplayAPI, ThreadsGraphAPI, ServerSentEventAPI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"access_token": "THQVJ...","token_type": "bearer", "expires_in": 5183944} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"access_token": "THQVJ...","user_id": 12345678} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
tests for threads graph api | ||
""" | ||
|
||
import responses | ||
|
||
from pyfacebook import ThreadsGraphAPI | ||
|
||
|
||
def test_threads_get_authorization_url(): | ||
api = ThreadsGraphAPI(app_id="id", app_secret="secret", oauth_flow=True) | ||
|
||
url, state = api.get_authorization_url(scope=["threads_basic"]) | ||
assert ( | ||
url | ||
== "https://threads.net/oauth/authorize?response_type=code&client_id=id&redirect_uri=https%3A%2F%2Flocalhost%2F&scope=threads_basic&state=PyFacebook" | ||
) | ||
|
||
|
||
def test_threads_exchange_user_access_token(helpers): | ||
api = ThreadsGraphAPI(app_id="id", app_secret="secret", oauth_flow=True) | ||
|
||
resp = "https://localhost/?code=code&state=PyFacebook#_" | ||
|
||
with responses.RequestsMock() as m: | ||
m.add( | ||
method=responses.POST, | ||
url=api.EXCHANGE_ACCESS_TOKEN_URL, | ||
json=helpers.load_json("testdata/base/threads_user_token.json"), | ||
) | ||
|
||
r = api.exchange_user_access_token(response=resp, scope=["threads_basic"]) | ||
assert r["access_token"] == "THQVJ..." | ||
|
||
|
||
def test_threads_exchange_long_lived_user_access_token(helpers): | ||
api = ThreadsGraphAPI(app_id="id", app_secret="secret", access_token="token") | ||
with responses.RequestsMock() as m: | ||
m.add( | ||
method=responses.GET, | ||
url=f"https://graph.threads.net/oauth/access_token", | ||
json=helpers.load_json("testdata/base/threads_user_long_lived_token.json"), | ||
) | ||
|
||
r = api.exchange_long_lived_user_access_token() | ||
assert r["access_token"] == "THQVJ..." | ||
|
||
|
||
def test_threads_refresh_access_token(helpers): | ||
api = ThreadsGraphAPI(app_id="id", app_secret="secret", access_token="token") | ||
with responses.RequestsMock() as m: | ||
m.add( | ||
method=responses.GET, | ||
url=f"https://graph.threads.net/refresh_access_token", | ||
json=helpers.load_json("testdata/base/threads_user_long_lived_token.json"), | ||
) | ||
|
||
r = api.refresh_access_token(access_token=api.access_token) | ||
assert r["access_token"] == "THQVJ..." |