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
)
| 608 | return url_concat(url, args) |
| 609 | |
| 610 | async def oauth2_request( |
| 611 | self, |
| 612 | url: str, |
| 613 | access_token: Optional[str] = None, |
| 614 | post_args: Optional[Dict[str, Any]] = None, |
| 615 | **args: Any |
| 616 | ) -> Any: |
| 617 | """Fetches the given URL auth an OAuth2 access token. |
| 618 | |
| 619 | If the request is a POST, ``post_args`` should be provided. Query |
| 620 | string arguments should be given as keyword arguments. |
| 621 | |
| 622 | Example usage: |
| 623 | |
| 624 | ..testcode:: |
| 625 | |
| 626 | class MainHandler(tornado.web.RequestHandler, |
| 627 | tornado.auth.FacebookGraphMixin): |
| 628 | @tornado.web.authenticated |
| 629 | async def get(self): |
| 630 | new_entry = await self.oauth2_request( |
| 631 | "https://graph.facebook.com/me/feed", |
| 632 | post_args={"message": "I am posting from my Tornado application!"}, |
| 633 | access_token=self.current_user["access_token"]) |
| 634 | |
| 635 | if not new_entry: |
| 636 | # Call failed; perhaps missing permission? |
| 637 | self.authorize_redirect() |
| 638 | return |
| 639 | self.finish("Posted a message!") |
| 640 | |
| 641 | .. testoutput:: |
| 642 | :hide: |
| 643 | |
| 644 | .. versionadded:: 4.3 |
| 645 | |
| 646 | .. versionchanged::: 6.0 |
| 647 | |
| 648 | The ``callback`` argument was removed. Use the returned awaitable object instead. |
| 649 | """ |
| 650 | all_args = {} |
| 651 | if access_token: |
| 652 | all_args["access_token"] = access_token |
| 653 | all_args.update(args) |
| 654 | |
| 655 | if all_args: |
| 656 | url += "?" + urllib.parse.urlencode(all_args) |
| 657 | http = self.get_auth_http_client() |
| 658 | if post_args is not None: |
| 659 | response = await http.fetch( |
| 660 | url, method="POST", body=urllib.parse.urlencode(post_args) |
| 661 | ) |
| 662 | else: |
| 663 | response = await http.fetch(url) |
| 664 | return escape.json_decode(response.body) |
| 665 | |
| 666 | def get_auth_http_client(self) -> httpclient.AsyncHTTPClient: |
| 667 | """Returns the `.AsyncHTTPClient` instance to be used for auth requests. |