(String[] args)
| 18 | */ |
| 19 | public class ExampleOAuth { |
| 20 | public static void main(String[] args) throws Exception { |
| 21 | if (System.getenv("ASANA_CLIENT_ID") == null || System.getenv("ASANA_CLIENT_SECRET") == null) { |
| 22 | throw new Error("Please set the ASANA_CLIENT_ID and ASANA_CLIENT_SECRET environment variables."); |
| 23 | } |
| 24 | |
| 25 | System.out.println("== Example using OAuth Client ID and Client Secret:"); |
| 26 | |
| 27 | // create an OAuth app with the OAuth credentials: |
| 28 | OAuthApp app = new OAuthApp( |
| 29 | System.getenv("ASANA_CLIENT_ID"), |
| 30 | System.getenv("ASANA_CLIENT_SECRET"), |
| 31 | // this special redirect URI will prompt the user to copy/paste the code. |
| 32 | // useful for command line scripts and other non-web apps |
| 33 | OAuthApp.NATIVE_REDIRECT_URI |
| 34 | ); |
| 35 | |
| 36 | // create an OAuth client with the app |
| 37 | Client client = Client.oauth(app); |
| 38 | |
| 39 | System.out.println("isAuthorized=" + app.isAuthorized()); |
| 40 | |
| 41 | // get an authorization URL: |
| 42 | String url = app.getAuthorizationUrl("FIXME: random state"); |
| 43 | System.out.println(url); |
| 44 | |
| 45 | // in a web app you'd redirect the user to this URL when they take action to |
| 46 | // login with Asana or connect their account to Asana |
| 47 | Desktop.getDesktop().browse(new URI(url)); |
| 48 | |
| 49 | // prompt the user to copy and paste the code from the browser window |
| 50 | System.out.println("Copy and paste the returned code from the browser and press enter:"); |
| 51 | String code = new LineReader(new InputStreamReader(System.in)).readLine(); |
| 52 | |
| 53 | // exchange the code for a bearer token |
| 54 | // normally you'd persist this token somewhere |
| 55 | String accessToken = app.fetchToken(code); |
| 56 | |
| 57 | System.out.println("isAuthorized=" + app.isAuthorized()); |
| 58 | System.out.println("token=" + accessToken); |
| 59 | |
| 60 | // get some information about your own user |
| 61 | User user = client.users.me().execute(); |
| 62 | System.out.println("me=" + user.name); |
| 63 | System.out.println(user.gid); |
| 64 | |
| 65 | // get your photo, if you have one |
| 66 | if (user.photo != null) { |
| 67 | System.out.println(user.photo.image_128x128); |
| 68 | } |
| 69 | |
| 70 | System.out.println(user.workspaces.iterator().next().name); |
| 71 | |
| 72 | // demonstrate creating a client using a previously obtained bearer token |
| 73 | System.out.println("== Example using OAuth Access Token:"); |
| 74 | app = new OAuthApp( |
| 75 | System.getenv("ASANA_CLIENT_ID"), |
| 76 | System.getenv("ASANA_CLIENT_SECRET"), |
| 77 | "urn:ietf:wg:oauth:2.0:oob", |
nothing calls this directly
no test coverage detected