Skip to content

Instantly share code, notes, and snippets.

@shin1ogawa
Created April 15, 2011 07:23
Show Gist options
  • Save shin1ogawa/921301 to your computer and use it in GitHub Desktop.
Save shin1ogawa/921301 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import org.junit.Test;
import com.google.gdata.client.GoogleService;
import com.google.gdata.client.appsforyourdomain.AppsPropertyService;
import com.google.gdata.client.appsforyourdomain.UserService;
import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthException;
import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
import com.google.gdata.client.authn.oauth.OAuthParameters.OAuthType;
import com.google.gdata.data.appsforyourdomain.AppsForYourDomainException;
import com.google.gdata.data.appsforyourdomain.generic.GenericEntry;
import com.google.gdata.data.appsforyourdomain.generic.GenericFeed;
import com.google.gdata.data.appsforyourdomain.provisioning.UserEntry;
import com.google.gdata.util.ServiceException;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* Provisioning API のサンプル。
* @author shin1ogawa
*/
public class ProvisioningAPITest {
static final String CONSUMER_KEY = "";
static final String CONSUMER_SECRET = "";
static final String DOMAINNAME = "example.com";
static final String GROUPEMAIL = "system@example.com";
static final String USEREMAIL = "shin1ogawa@example.com";
/**
* ドメイン内の全グループを取得するサンプル。
* @throws IOException
* @throws OAuthException
*/
@Test
public void retrieveAllGroups() throws IOException, OAuthException {
AppsPropertyService service = new AppsPropertyService("ProvisioningTest");
prepareService(service);
List<GenericEntry> entries =
GDataAPIUtil.getFeedsWithRetry(
service,
new URL("https://apps-apis.google.com/a/feeds/group/2.0/"
+ URLEncoder.encode(DOMAINNAME, "utf-8")), GenericFeed.class)
.getEntries();
for (GenericEntry entry : entries) {
System.out.println(entry.getProperty("groupName") + " groupId="
+ entry.getProperty("groupId"));
}
assertThat(entries.size(), is(greaterThan(0)));
}
/**
* グループ内の全メンバを取得するサンプル。
* @throws IOException
* @throws OAuthException
*/
@Test
public void retrieveAllMembersInGroup() throws IOException, OAuthException {
AppsPropertyService service = new AppsPropertyService("ProvisioningTest");
prepareService(service);
List<GenericEntry> entries =
GDataAPIUtil.getFeedsWithRetry(
service,
new URL("https://apps-apis.google.com/a/feeds/group/2.0/"
+ URLEncoder.encode(DOMAINNAME, "utf-8") + "/"
+ URLEncoder.encode(GROUPEMAIL, "utf-8") + "/member"),
GenericFeed.class).getEntries();
for (GenericEntry entry : entries) {
System.out.println(entry.getProperty("memberId") + ", memberType="
+ entry.getProperty("memberType") + ", directMember="
+ entry.getProperty("directMember"));
}
assertThat(entries.size(), is(greaterThan(0)));
}
/**
* 特定のユーザの詳細情報を取得するサンプル。
* @throws ServiceException
* @throws IOException
* @throws AppsForYourDomainException
* @throws OAuthException
*/
@Test
public void retrieveUser() throws IOException, AppsForYourDomainException, ServiceException,
OAuthException {
UserService service = new UserService("ProvisioningTest");
prepareService(service);
int indexOf = USEREMAIL.indexOf('@');
String userName = indexOf > 0 ? USEREMAIL.substring(0, indexOf) : USEREMAIL;
UserEntry entry =
service.getEntry(
new URL(new StringBuilder("https://apps-apis.google.com/a/feeds/")
.append(URLEncoder.encode(DOMAINNAME, "utf-8")).append("/user/2.0/")
.append(userName).toString()), UserEntry.class);
assertThat(entry, is(notNullValue()));
assertThat(entry.getLogin(), is(notNullValue()));
assertThat(entry.getName(), is(notNullValue()));
System.out.println(entry.getLogin().getUserName() + ", isAdmin="
+ entry.getLogin().getAdmin() + ", familyName=" + entry.getName().getFamilyName()
+ ", givenName=" + entry.getName().getGivenName());
}
/**
* {@code #service}を準備する。
* @param service GoogleService
* @throws OAuthException
*/
static void prepareService(GoogleService service) throws OAuthException {
GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
oauthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH);
service.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
service.useSsl();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment