本文整理汇总了Java中com.google.api.client.googleapis.javanet.GoogleNetHttpTransport类的典型用法代码示例。如果您正苦于以下问题:Java GoogleNetHttpTransport类的具体用法?Java GoogleNetHttpTransport怎么用?Java GoogleNetHttpTransport使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
GoogleNetHttpTransport类属于com.google.api.client.googleapis.javanet包,在下文中一共展示了GoogleNetHttpTransport类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: performRequest
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
private static void performRequest(String accessToken, String refreshToken, String apiKey, String apiSecret) throws GeneralSecurityException, IOException, MessagingException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = new JacksonFactory();
final Credential credential = convertToGoogleCredential(accessToken, refreshToken, apiSecret, apiKey);
Builder builder = new Gmail.Builder(httpTransport, jsonFactory, credential);
builder.setApplicationName("OAuth API Sample");
Gmail gmail = builder.build();
MimeMessage content = createEmail("[email protected]", "[email protected]", "Test Email", "It works");
Message message = createMessageWithEmail(content);
gmail.users().messages().send("[email protected]", message).execute();
}
示例2: doLogin
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
public static void doLogin(String user){
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
Credential credential = authorize(user);
oauth2 = new Oauth2.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
String picture = oauth2.userinfo().get().execute().getPicture();
//name = oauth2.userinfo().get().execute().getName();
downloadProfileImage(picture,user.toLowerCase());
} catch (IOException e) {
System.err.println(e.getMessage());
} catch (Exception t) {
}
}
示例3: createClient
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
/**
* Setup authorization for local app based on private key.
* See <a href="https://cloud.google.com/pubsub/configure">cloud.google.com/pubsub/configure</a>
*/
private void createClient(String private_key_file, String email) throws IOException, GeneralSecurityException {
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(transport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountScopes(PubsubScopes.all())
.setServiceAccountId(email)
.setServiceAccountPrivateKeyFromP12File(new File(private_key_file))
.build();
// Please use custom HttpRequestInitializer for automatic retry upon failures.
// HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential);
pubsub = new Pubsub.Builder(transport, JSON_FACTORY, credential)
.setApplicationName("eaipubsub")
.build();
}
示例4: GoogleConnector
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
/**
* Instances the google connector configuring all required resources.
*/
private GoogleConnector() throws IOException, GeneralSecurityException {
super();
// 1. JSON library
jsonFactory = JacksonFactory.getDefaultInstance();
// 2. Configure HTTP transport
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// 3. Load the credentials
secrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(GoogleConnector.class.getResourceAsStream("client-secrets.json")));
// 4. Configure the authentication flow
dataStoreFactory = new FileDataStoreFactory(CREDENTIALS_DIRECTORY);
// 5. Create flow
imp_buildAuthorizationFlow();
}
示例5: getProjectsApiStub
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
/**
* Return the Projects api object used for accessing the Cloud Resource Manager Projects API.
* @return Projects api object used for accessing the Cloud Resource Manager Projects API
* @throws GeneralSecurityException Thrown if there's a permissions error.
* @throws IOException Thrown if there's an IO error initializing the API object.
*/
public static synchronized Projects getProjectsApiStub()
throws GeneralSecurityException, IOException {
if (projectApiStub != null) {
return projectApiStub;
}
HttpTransport transport;
GoogleCredential credential;
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
transport = GoogleNetHttpTransport.newTrustedTransport();
credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
if (credential.createScopedRequired()) {
Collection<String> scopes = CloudResourceManagerScopes.all();
credential = credential.createScoped(scopes);
}
projectApiStub = new CloudResourceManager
.Builder(transport, jsonFactory, credential)
.build()
.projects();
return projectApiStub;
}
示例6: getServiceAccountsApiStub
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
/**
* Get the API stub for accessing the IAM Service Accounts API.
* @return ServiceAccounts api stub for accessing the IAM Service Accounts API.
* @throws IOException Thrown if there's an IO error initializing the api connection.
* @throws GeneralSecurityException Thrown if there's a security error
* initializing the connection.
*/
public static ServiceAccounts getServiceAccountsApiStub() throws IOException, GeneralSecurityException {
if (serviceAccountsApiStub == null) {
HttpTransport transport;
GoogleCredential credential;
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
transport = GoogleNetHttpTransport.newTrustedTransport();
credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
if (credential.createScopedRequired()) {
Collection<String> scopes = IamScopes.all();
credential = credential.createScoped(scopes);
}
serviceAccountsApiStub = new Iam.Builder(transport, jsonFactory, credential)
.build()
.projects()
.serviceAccounts();
}
return serviceAccountsApiStub;
}
示例7: createServiceAccountKeyManager
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
private static ServiceAccountKeyManager createServiceAccountKeyManager() {
try {
final HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
final JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
final GoogleCredential credential = GoogleCredential
.getApplicationDefault(httpTransport, jsonFactory)
.createScoped(IamScopes.all());
final Iam iam = new Iam.Builder(
httpTransport, jsonFactory, credential)
.setApplicationName(SERVICE_NAME)
.build();
return new ServiceAccountKeyManager(iam);
} catch (GeneralSecurityException | IOException e) {
throw new RuntimeException(e);
}
}
示例8: build
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
@Override
public DataprocHadoopRunner build() {
CredentialProvider credentialProvider = Optional.ofNullable(this.credentialProvider)
.orElseGet(DefaultCredentialProvider::new);
try {
final HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
final JsonFactory jsonFactory = new JacksonFactory();
final GoogleCredential credentials = credentialProvider.getCredential(SCOPES);
final Dataproc dataproc = new Dataproc.Builder(httpTransport, jsonFactory, credentials)
.setApplicationName(APPLICATION_NAME).build();
final Storage storage = new Storage.Builder(httpTransport, jsonFactory, credentials)
.setApplicationName(APPLICATION_NAME).build();
final DataprocClient dataprocClient =
new DataprocClient(dataproc, storage, projectId, clusterId, clusterProperties);
return new DataprocHadoopRunnerImpl(dataprocClient);
} catch (Throwable e) {
throw Throwables.propagate(e);
}
}
示例9: getGoogleComputeClient
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
/**
* Get a Google Compute Engine client object.
* @param userEmail The service account's client email.
* @param privateKey The service account's private key.
* @param scopes The scopes used in the compute client object.
* @param applicationName The application name.
* @return The created Compute Engine client object.
* @throws GeneralSecurityException Exception when creating http transport.
* @throws IOException Exception when creating http transport.
*/
public static Compute getGoogleComputeClient(String userEmail,
String privateKey,
List<String> scopes,
String applicationName)
throws GeneralSecurityException, IOException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(userEmail)
.setServiceAccountScopes(scopes)
.setServiceAccountPrivateKey(privateKeyFromPkcs8(privateKey))
.build();
return new Compute.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(applicationName)
.build();
}
示例10: getSensorEndpoint
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
/**
* Create a Sensor API client instance
* @return
* @throws IOException
* @throws GeneralSecurityException
*/
private static Sensor getSensorEndpoint() throws IOException {
CmdLineAuthenticationProvider provider = new CmdLineAuthenticationProvider();
// see https://developers.google.com/api-client-library/java/
provider.setClientSecretsFile("client_secret.json");
provider.setScopes(SCOPES);
// get the oauth credentials
Credential credential = provider.authorize();
// initialize the transport
HttpTransport httpTransport;
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
} catch (GeneralSecurityException e) {
log.log(Level.SEVERE, "failed to create transport", e);
throw new IOException(e);
}
return new Sensor.Builder(httpTransport, JacksonFactory.getDefaultInstance(), credential)
.setApplicationName(APP_NAME).setRootUrl(DEFAULT_ROOT_URL).build();
}
示例11: createAdminApiClient
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
private static SQLAdmin createAdminApiClient(Credential credential) {
HttpTransport httpTransport;
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
} catch (GeneralSecurityException | IOException e) {
throw new RuntimeException("Unable to initialize HTTP transport", e);
}
String rootUrl = System.getProperty(API_ROOT_URL_PROPERTY);
String servicePath = System.getProperty(API_SERVICE_PATH_PROPERTY);
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
SQLAdmin.Builder adminApiBuilder =
new Builder(httpTransport, jsonFactory, credential)
.setApplicationName("Cloud SQL Java Socket Factory");
if (rootUrl != null) {
logTestPropertyWarning(API_ROOT_URL_PROPERTY);
adminApiBuilder.setRootUrl(rootUrl);
}
if (servicePath != null) {
logTestPropertyWarning(API_SERVICE_PATH_PROPERTY);
adminApiBuilder.setServicePath(servicePath);
}
return adminApiBuilder.build();
}
示例12: getApiService
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
/**
* Get an instance of the language service.
*
* @param document
* @return
*/
public CloudNaturalLanguageAPI getApiService(GoogleCredential credential) {
final GoogleCredential cred = credential;
CloudNaturalLanguageAPI api = null;
try {
api = new CloudNaturalLanguageAPI.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(),
new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
cred.initialize(httpRequest);
}
}).setApplicationName(getApplicationName()).build();
} catch (Exception ex) {
throw new GateRuntimeException("Could not establish Google Service API", ex);
}
//System.err.println("DEBUG: API instance established: " + api);
return api;
}
示例13: init
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
public GoogleCalendarSync init() throws Exception {
// initialize the transport
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// initialize the data store factory
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
// authorization
final Credential credential = authorize();
// set up global Calendar instance
client = new com.google.api.services.calendar.Calendar.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
return this;
}
示例14: authorize
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
private Sheets authorize() {
try {
InputStream in = new FileInputStream(new File(System.getenv("GOOGLE_OATH2_CREDENTIALS")));
JsonFactory factory = new JacksonFactory();
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(factory, new InputStreamReader(in, Charset.defaultCharset()));
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
FileDataStoreFactory dataStoreFactory =
new FileDataStoreFactory(new File(dataStoreDirectory));
List<String> scopes = Collections.singletonList(SheetsScopes.SPREADSHEETS);
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(transport, factory, clientSecrets, scopes)
.setAccessType("offline")
.setDataStoreFactory(dataStoreFactory)
.build();
Credential credential =
new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
return new Sheets.Builder(transport, factory, credential)
.setApplicationName(APPLICATION_NAME)
.build();
} catch (Exception e) {
return null;
}
}
示例15: getGmailService
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //导入依赖的package包/类
private static Gmail getGmailService(String basedir, String appName) throws Exception {
// 機密情報ファイルのパス
File DATA_STORE_DIR = new java.io.File(basedir, "gmail-secrets");
File SECRET_JSON = new java.io.File(DATA_STORE_DIR, "client_secret.json");
// 準備
FileDataStoreFactory DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
// 送信のみ
List<String> SCOPES = Arrays.asList(GmailScopes.GMAIL_SEND);
// Credential取得
try (InputStream in = FileUtils.openInputStream(SECRET_JSON)) {
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
// Gmailインスタンス生成
return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(appName).build();
}
}