Created
December 25, 2012 16:57
-
-
Save anonymous/4374186 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2011 Google Inc. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | |
* in compliance with the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software distributed under the License | |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | |
* or implied. See the License for the specific language governing permissions and limitations under | |
* the License. | |
*/ | |
package com.google.api.services.samples.analytics.cmdline; | |
import com.google.api.client.auth.oauth2.Credential; | |
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; | |
import com.google.api.client.extensions.java6.auth.oauth2.FileCredentialStore; | |
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; | |
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; | |
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; | |
import com.google.api.client.googleapis.json.GoogleJsonResponseException; | |
import com.google.api.client.http.HttpTransport; | |
import com.google.api.client.http.InputStreamContent; | |
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.analytics.Analytics; | |
import com.google.api.services.analytics.Analytics.Management.DailyUploads.Upload; | |
import com.google.api.services.analytics.AnalyticsScopes; | |
import com.google.api.services.analytics.model.DailyUploadAppend; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.util.Collections; | |
/** | |
* @author [email protected] (Your Name Here) | |
* | |
*/ | |
public class MyUpload { | |
/** Global instance of the HTTP transport. */ | |
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); | |
/** Global instance of the JSON factory. */ | |
private static final JsonFactory JSON_FACTORY = new JacksonFactory(); | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
try { | |
Analytics analytics = initializeAnalytics(); | |
File file = new File("mycosts.csv"); | |
InputStreamContent mediaContent = new InputStreamContent("application/octet-stream", new FileInputStream(file)); | |
mediaContent.setLength(file.length()); | |
Upload upload = analytics.management().dailyUploads().upload("1234", | |
"UA-1234-1", "my_cost_id", "2012-12-23", 1, "cost", mediaContent); | |
upload.setReset(true); | |
DailyUploadAppend append = upload.execute(); | |
System.out.println("Account ID:" + append.getAccountId()); | |
System.out.println("Web Property ID:" + append.getWebPropertyId()); | |
System.out.println("Custom Data Source ID:" + append.getCustomDataSourceId()); | |
System.out.println("Date:" + append.getDate()); | |
System.out.println("Append Number:" + append.getAppendNumber()); | |
System.out.println("Next Append Link:" + append.getNextAppendLink()); | |
} catch (GoogleJsonResponseException e) { | |
System.err.println("There was a service error: " | |
+ e.getDetails().getCode() + " : " | |
+ e.getDetails().getMessage()); | |
} catch (IOException exception) { | |
// TODO Auto-generated catch block | |
exception.printStackTrace(); | |
} catch (Exception exception) { | |
// TODO Auto-generated catch block | |
exception.printStackTrace(); | |
} | |
} | |
/** | |
* Performs all necessary setup steps for running requests against the API. | |
* | |
* @return An initialized Analytics service object. | |
* | |
* @throws Exception if an issue occurs with OAuth2Native authorize. | |
*/ | |
private static Analytics initializeAnalytics() throws Exception { | |
// Authorization. | |
Credential credential = authorize(); | |
// Set up and return Google Analytics API client. | |
return new Analytics.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName( | |
"Analytics Project API Project").build(); | |
} | |
/** Authorizes the installed application to access user's protected data. */ | |
private static Credential authorize() throws Exception { | |
// load client secrets | |
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load( | |
JSON_FACTORY, HelloAnalyticsApiSample.class.getResourceAsStream("/client_secrets.json")); | |
// set up file credential store | |
FileCredentialStore credentialStore = new FileCredentialStore( | |
new File(System.getProperty("user.home"), ".credentials/analytics.json"), JSON_FACTORY); | |
// set up authorization code flow | |
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( | |
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, | |
Collections.singleton(AnalyticsScopes.ANALYTICS)).setCredentialStore( | |
credentialStore).build(); | |
// authorize | |
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment