Skip to content

Instantly share code, notes, and snippets.

@q-litzler
Created February 11, 2018 19:16
Show Gist options
  • Save q-litzler/a67dfe50d35c7ec1e3bc50b953bc2d43 to your computer and use it in GitHub Desktop.
Save q-litzler/a67dfe50d35c7ec1e3bc50b953bc2d43 to your computer and use it in GitHub Desktop.
class ActivityHome : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val transport = NetHttpTransport()
val jsonFactory = JacksonFactory.getDefaultInstance()
val request = YouTubeRequestInitializer("my-youtube-api-key-3")
val service = YouTube.Builder(
transport, jsonFactory, HttpRequestInitializer { request ->
val packageName = packageName
val SHA1 = getSHA1(packageName)
request.headers.set("X-Android-Package", packageName)
request.headers.set("X-Android-Cert", SHA1)
})
.setYouTubeRequestInitializer(request)
.setApplicationName("Tube")
.build()
launch {
val response = service
.search()
.list("snippet,id")
.setMaxResults(50)
.set("channelId", "UCT67YOMntJxfRnO_9bXDpvw")
.setOrder("date")
.execute()
System.out.println(response)
}
}
private fun getSHA1(packageName: String): String? {
try {
val signatures = packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures
for (signature in signatures) {
val md: MessageDigest = MessageDigest.getInstance("SHA-1")
md.update(signature.toByteArray())
return BaseEncoding.base16().encode(md.digest())
}
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
}
return null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment