Commits

Giuliano Ribeiro  committed e7b0bea

novo construtor.

  • Participants
  • Parent commits 454bb97

Comments (0)

Files changed (1)

File Google/gmail-settings/src/main/java/com/ilegra/google/apps/api/GmailSettings.java

 import com.google.api.client.json.JsonFactory;
 import com.google.api.client.json.jackson.JacksonFactory;
 import com.google.api.services.oauth2.Oauth2;
+import com.google.api.services.oauth2.model.Userinfo;
 import com.google.api.services.samples.shared.cmdline.oauth2.LocalServerReceiver;
 import com.google.api.services.samples.shared.cmdline.oauth2.OAuth2Native;
 import com.google.gdata.client.appsforyourdomain.AppsForYourDomainQuery;
 		this.password = password;
 		new GenericFeed().declareExtensions(getExtensionProfile());
 		// ---------------------------------------------
-		// authorization
-		Credential credential = OAuth2Native.authorize(HTTP_TRANSPORT, JSON_FACTORY, new LocalServerReceiver(), SCOPES);
-		// set up global Oauth2 instance
-		// oauth2 = new Oauth2.Builder(HTTP_TRANSPORT, JSON_FACTORY,
-		// credential).setApplicationName("Google-OAuth2Sample/1.0").build();
-		// ---------------------------------------------
-		// this.setUserCredentials(username + "@" + domain, password);
-		this.setOAuth2Credentials(credential);
+		this.setUserCredentials(username + "@" + domain, password);
 	}
 
+    /**
+     * Constructs a GmailSettings for the given domain using the given admin
+     * credentials.
+     *
+     * @param applicationName
+     *            the name of the application making the modifications.
+     * @throws Exception
+     */
+    public GmailSettings(String applicationName) throws Exception {
+        super(applicationName, Constants.PROTOCOL, Constants.APPS_APIS_DOMAIN);
+        new GenericFeed().declareExtensions(getExtensionProfile());
+        // ---------------------------------------------
+        Credential credential = OAuth2Native.authorize(HTTP_TRANSPORT, JSON_FACTORY, new LocalServerReceiver(), SCOPES);
+        oauth2 = new Oauth2.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(applicationName).build();
+        this.setOAuth2Credentials(credential);
+        // ---------------------------------------------
+        Userinfo userinfo = oauth2.userinfo().get().execute();
+        String[] tmp = userinfo.getEmail().split("@");
+        this.domain = tmp[1];
+        this.username = tmp[0];
+        this.password = null;
+    }
+
 	/**
 	 * Retrieves signature
 	 * 
 		URL singleUrl = buildSettingsUrl(username, setting);
 		return update(singleUrl, entry);
 	}
+
+    public String getDomain() {
+        return domain;
+    }
+
+    public String getUsername() {
+        return username;
+    }
 }