错误:使用Java连接Google Cloud Storage时=> java.net.UnknownHostException:oauth2.googleapis.com

时间:2019-04-03 10:34:59

标签: java oauth google-cloud-storage google-oauth

使用Java库连接到Google Cloud Storage时,出现以下异常:

  1. 原因:java.net.UnknownHostException:oauth2.googleapis.com
  2. 原因:java.io.IOException:获取服务帐户的访问令牌时出错:oauth2.googleapis.com

我们已经创建了Google Cloud Storage服务帐户Project,还下载了JSON文件。

下面是代码段:

struct Movie: Codable
{
   let trackId: Int
   let trackName: String
   let trackGenre: String
   let trackPrice: Int?
   let longDescription: String

   init(trackId: Int, trackName: String, trackGenre: String, 
   trackPrice: 
   Int?, /*trackImage: Thumbnail,*/ longDescription: String)
   {
    self.trackId = trackId
    self.trackName = trackName
    self.trackGenre = trackGenre
    self.trackPrice = trackPrice ?? 0
    self.longDescription = longDescription
    //self.trackImage = trackImage //TODO: thumbnail: mapp url from json
   }

  private enum MovieCodingKeys: String, CodingKey
  {
    case trackId
    case trackName
    case trackGenre = "primaryGenreName"
    case trackPrice
    //case trackImage
    case longDescription
  }

  init(from decoder: Decoder) throws
  {
    let container = try decoder.container(keyedBy: MovieCodingKeys.self)

    trackId = try container.decode(Int.self, forKey: .trackId)
    trackName = try container.decode(String.self, forKey: .trackName)
    trackGenre = try container.decode(String.self, forKey: .trackGenre)
    trackPrice = try container.decode(Int?.self, forKey: .trackPrice)
    longDescription = try container.decode(String.self, forKey: .longDescription)
  }
}

1 个答案:

答案 0 :(得分:0)

这可能是互联网的要求。

尝试:Android Studio:<uses-permission android:name="android.permission.INTERNET" />在Android清单上

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" />

在Android清单上