简体   繁体   中英

Error: while connecting Google Cloud Storage using Java => java.net.UnknownHostException: oauth2.googleapis.com

While connecting to Google Cloud Storage using Java libraries, We are getting below exception :

  1. Caused by: java.net.UnknownHostException: oauth2.googleapis.com
  2. Caused by: java.io.IOException: Error getting access token for service account: oauth2.googleapis.com

We have created the Google Cloud Storage service account, Project and downloaded JSON file also.

Below is the code snippet :

GoogleCredentials googleCredentails = 
            ServiceAccountCredentials.fromStream(new FileInputStream("D:\\Naresh\\Projects\\GoogleCloudStorage\\delta-trees-236311-ae7c1cfcda68.json"));

Storage googleStorageService = StorageOptions.newBuilder().setCredentials(googleCredentails)
            .setProjectId("delta-trees-236311")
            .build().getService();

String googleBucketName = "ewf-ocr-poc";
File f = new File("D:\\Naresh\\Projects\\GoogleCloudStorage\\EWFDocumentThroughJava.txt");
byte[] readFileToByteArray = readFileToByteArray(f);
String fileName = f.getName();
InputStream fis = new FileInputStream(f);        
googleStorageService.create(BlobInfo.newBuilder(googleBucketName, fileName).build(),
            readFileToByteArray);

It could be the internet pemission.

try: Android studio: <uses-permission android:name="android.permission.INTERNET" /> On Android Manifest

Xamarin:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
         <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

On Android Manifest

Looks like you need to add your https proxy in the java app to make your connection to gcp host work. You can add is using below system properties:

    System.setProperty("https.proxyHost", yourProxyHost);
    System.setProperty("https.proxyPort", yourProxyPort);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM