|
| 1 | +package com.flickr4java.flickr.test; |
| 2 | + |
| 3 | +import com.flickr4java.flickr.Flickr; |
| 4 | +import com.flickr4java.flickr.FlickrException; |
| 5 | +import com.flickr4java.flickr.REST; |
| 6 | +import com.flickr4java.flickr.auth.Auth; |
| 7 | +import com.flickr4java.flickr.auth.AuthInterface; |
| 8 | +import com.flickr4java.flickr.auth.Permission; |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.io.FileOutputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.Properties; |
| 13 | +import java.util.Scanner; |
| 14 | +import org.scribe.model.Token; |
| 15 | +import org.scribe.model.Verifier; |
| 16 | + |
| 17 | +public class Setup { |
| 18 | + |
| 19 | + public static void main(String[] args) { |
| 20 | + System.out.println("Flickr4Java: Set up integration test environment"); |
| 21 | + try { |
| 22 | + new Setup(); |
| 23 | + } catch (IOException e) { |
| 24 | + e.printStackTrace(); |
| 25 | + } catch (FlickrException e) { |
| 26 | + e.printStackTrace(); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public Setup() throws IOException, FlickrException { |
| 31 | + String propertiesFile = "src/test/resources/setup.properties"; |
| 32 | + Properties properties = new Properties(); |
| 33 | + properties.load(new FileInputStream(propertiesFile)); |
| 34 | + |
| 35 | + Flickr flickr = new Flickr(properties.getProperty("apiKey"), properties.getProperty("secret"), new REST()); |
| 36 | + Flickr.debugStream = false; |
| 37 | + AuthInterface authInterface = flickr.getAuthInterface(); |
| 38 | + |
| 39 | + Scanner scanner = new Scanner(System.in); |
| 40 | + |
| 41 | + Token requestToken = authInterface.getRequestToken(); |
| 42 | + |
| 43 | + String url = authInterface.getAuthorizationUrl(requestToken, Permission.DELETE); |
| 44 | + System.out.println("Follow this URL to authorise yourself on Flickr"); |
| 45 | + System.out.println(url); |
| 46 | + System.out.println("Paste in the token it gives you:"); |
| 47 | + System.out.print(">> "); |
| 48 | + |
| 49 | + String tokenKey = scanner.nextLine().trim(); |
| 50 | + |
| 51 | + Token accessToken = authInterface.getAccessToken(requestToken, new Verifier(tokenKey)); |
| 52 | + System.out.println("Authentication success"); |
| 53 | + |
| 54 | + Auth auth = authInterface.checkToken(accessToken); |
| 55 | + |
| 56 | + properties.setProperty("token", accessToken.getToken()); |
| 57 | + properties.setProperty("tokensecret", accessToken.getSecret()); |
| 58 | + properties.store(new FileOutputStream(propertiesFile), ""); |
| 59 | + |
| 60 | + // This token can be used until the user revokes it. |
| 61 | + System.out.println("Access token - token = " + accessToken.getToken()); |
| 62 | + System.out.println(" - secret = " + accessToken.getSecret()); |
| 63 | + System.out.println("(These have been saved to the properties file.)"); |
| 64 | + System.out.println("Realname: " + auth.getUser().getRealName()); |
| 65 | + System.out.println("Username: " + auth.getUser().getUsername()); |
| 66 | + System.out.println("Permission: " + auth.getPermission().getType()); |
| 67 | + } |
| 68 | +} |
0 commit comments