Fetches the given URL auth an OAuth2 access token. If the request is a POST, ``post_args`` should be provided. Query string arguments should be given as keyword arguments. Example usage: ..testcode:: class MainHandler(tornado.web.RequestHandler,
(
self,
url: str,
access_token: Optional[str] = None,
post_args: Optional[Dict[str, Any]] = None,
**args: Any,
)
| 629 | return url_concat(url, args) |
| 630 | |
| 631 | async def oauth2_request( |
| 632 | self, |
| 633 | url: str, |
| 634 | access_token: Optional[str] = None, |
| 635 | post_args: Optional[Dict[str, Any]] = None, |
| 636 | **args: Any, |
| 637 | ) -> Any: |
| 638 | """Fetches the given URL auth an OAuth2 access token. |
| 639 | |
| 640 | If the request is a POST, ``post_args`` should be provided. Query |
| 641 | string arguments should be given as keyword arguments. |
| 642 | |
| 643 | Example usage: |
| 644 | |
| 645 | ..testcode:: |
| 646 | |
| 647 | class MainHandler(tornado.web.RequestHandler, |
| 648 | tornado.auth.FacebookGraphMixin): |
| 649 | @tornado.web.authenticated |
| 650 | async def get(self): |
| 651 | new_entry = await self.oauth2_request( |
| 652 | "https://graph.facebook.com/me/feed", |
| 653 | post_args={"message": "I am posting from my Tornado application!"}, |
| 654 | access_token=self.current_user["access_token"]) |
| 655 | |
| 656 | if not new_entry: |
| 657 | # Call failed; perhaps missing permission? |
| 658 | self.authorize_redirect() |
| 659 | return |
| 660 | self.finish("Posted a message!") |
| 661 | |
| 662 | .. versionadded:: 4.3 |
| 663 | |
| 664 | .. versionchanged::: 6.0 |
| 665 | |
| 666 | The ``callback`` argument was removed. Use the returned awaitable object instead. |
| 667 | """ |
| 668 | all_args = {} |
| 669 | if access_token: |
| 670 | all_args["access_token"] = access_token |
| 671 | all_args.update(args) |
| 672 | |
| 673 | if all_args: |
| 674 | url += "?" + urllib.parse.urlencode(all_args) |
| 675 | http = self.get_auth_http_client() |
| 676 | if post_args is not None: |
| 677 | response = await http.fetch( |
| 678 | url, method="POST", body=urllib.parse.urlencode(post_args) |
| 679 | ) |
| 680 | else: |
| 681 | response = await http.fetch(url) |
| 682 | return escape.json_decode(response.body) |
| 683 | |
| 684 | def get_auth_http_client(self) -> httpclient.AsyncHTTPClient: |
| 685 | """Returns the `.AsyncHTTPClient` instance to be used for auth requests. |