oumaymakouraichi
-
Modifié par KX le 20/03/2015 à 21:14
dsy73
Messages postés9252Date d'inscriptiondimanche 22 août 2010StatutContributeurDernière intervention23 octobre 2020
-
21 mars 2015 à 10:45
Bonjour à tous ,voilà je suis entrain de développer une application google app engine sous java EE pour qu'on puisse développer sdk report admin, mon encadrant de stage avait appelé l'assistant de google qui nous aà renseigné enfin d'utiliser le code qui est déjà programmé par leurs développeurs, mais lors de l'exécution de l'application je reçois ce message d'erreur (Error: HTTP method GET is not supported by this URL)
voilà le code utilisé:
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.admin.reports.Reports;
import com.google.api.services.admin.reports.ReportsScopes;
import com.google.api.services.admin.reports.model.Activity;
import com.google.api.services.admin.reports.model.Activities;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
public class ReportsCommandLine {
private static String CLIENT_ID = "YOUR_CLIENT_ID";
private static String CLIENT_SECRET = "YOUR_CLIENT_SECRET";
private static String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
public static void main(String[] args) throws IOException {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(ReportsScopes.ADMIN_REPORTS_AUDIT_READONLY))
.setAccessType("online")
.setApprovalPrompt("auto").build();
String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
System.out.println("Please open the following URL in your browser then type the authorization code:");
System.out.println(" " + url);
System.out.println("Enter authorization code:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = br.readLine();
GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);
// Create a new authorized API client
Reports service = new Reports.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("ReportsCommandLine")
.build();
// Set start time to one week ago, to avoid too many results
long SEVEN_DAYS_IN_MS = 1000 * 60 * 60 * 24 * 7;
String startTime = formatDateToRfc3339(
new Date(System.currentTimeMillis() - SEVEN_DAYS_IN_MS));
List<Activity> allLogins = new ArrayList<Activity>();
Reports.Activities.List request = service.activities().list("all", "login")
.setStartTime(startTime);
// Get all activities
do {
try {
Activities currentPage = request.execute();
allLogins.addAll(currentPage.getItems());
request.setPageToken(currentPage.getNextPageToken());
} catch (IOException e) {
System.out.println("An error occurred: " + e);
request.setPageToken(null);
}
} while (request.getPageToken() != null &&
request.getPageToken().length() > 0 );
// Print all activities
for (Activity currentActivity : allLogins) {
for (Activity.Events currentEvent : currentActivity.getEvents()) {
String message = String.format(
"%s from %s", currentEvent.getType(), currentActivity.getActor().getEmail());
System.out.println(message);
}
}
}
// Format a Date to RFC 3339 as required by the Reports API
private static String formatDateToRfc3339(Date date) {
TimeZone utc = TimeZone.getTimeZone("UTC");
SimpleDateFormat rfc3339 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
rfc3339.setTimeZone(utc);
return rfc3339.format(date);
}
}