Dal*_*ale 6 android public-folders google-api-client google-drive-api google-drive-android-api
我希望我的应用能够从预定义的共享公共Google云端硬盘文件夹中读取,而无需用户登录或选择Google帐户.
使用我的桌面浏览器,我在Google云端硬盘上创建了一个公共文件夹,该文件夹设置为公开.有链接的任何人都可以访问(读取)驱动器,因此无需授权:
在我的Android Studio项目中,我已经进入File > Project Structure > Dependencies并添加了com.google.android.gms:play-services-drive:10.2.0
我现在有能力创建一个new GoogleApiClient.Builder().
我查看了各种示例,但在大多数情况下,驱动程序是由Android应用程序创建的.这不是我想要管理的情况.
此问题是关于访问已使用"文件夹ID"公开的驱动器,或者0B6X74x23H....在最初共享和公开文件夹时分配的任何内容.
我已经检查了Google提供的演示代码,但据推测,这不是公共文件夹,因为它说:
...需要注册OAuth 2.0客户端
至少,我可以使用http-client来驱动这个过程,在https://drive.google.com/drive/folders/0B6X74x23Hx7DNE13M0ZIbVI....?usp=sharing没有身份验证的情况下进入共享链接,而不需要跳过箍.但是,当然使用已定义的API并简单地指定公共共享文件夹以列出内容并且如果需要,从公共文件夹下载文件将更加清晰.
当我尝试这段代码时:
Scope publicFolder = new Scope(EXISTING_FOLDER_ID);
mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
.addApi(Drive.API)
.addScope(publicFolder)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
Run Code Online (Sandbox Code Playgroud)
这种方法触发:
GoogleApiClient.OnConnectionFailedListener.onConnectionFailed()
结果包含statusCode=SIGN_IN_REQUIRED.但当然,对于公开的文件夹,不需要登录.
小智 2
这是 Jsoup 的解决方法
implementation 'org.jsoup:jsoup:1.11.3'
val url = "https://drive.google.com/drive/folders/xxxxxxxxxxxxxxxxxx" // shared folder link
val doc = Jsoup.connect(url).get()
doc.outputSettings().prettyPrint(false)
val files = doc.select("div.WYuW0e")
for (file in files){
val fileName = file.text()
val fileID = file.attr("data-id")
val downloadLink = "https://drive.google.com/uc?export=download&id=$fileID"
//the downloadLink may open a 'Google Drive can't scan this file for viruses' page
// below we check for the new link
val doc2 = Jsoup.connect(downloadLink).get()
doc2.outputSettings().prettyPrint(false)
val elem = doc2.select("[id='uc-download-link']")
val newLink = if (elem.size != 0){
"https://drive.google.com" + elem.first().attr("href")
} else {
downloadLink
}
}
Run Code Online (Sandbox Code Playgroud)