我正在尝试从我的Android应用中显示用户在其Google云端硬盘上的文件列表,一旦用户选择了文件,我想获得Bearer token
和REST
该帐户将其提供给我的应用程序服务器下载它。
我一直在环顾四周,看起来很混乱。 Google云端硬盘有2个SDK,Working JSFiddle here.和Android SDK(downloadUrl
)。
我成功地获取了文件列表并使用Android SDK显示它(我没有构建任何UI),当用户选择文件时,我获得了所有Java SDK关于文件,但webContentLink
。我确实得到了alternateLink
和downloadUrl
之类的链接,但事实证明,由于文件未共享,我无法将这些链接传递给我的服务器进行下载。
经过一些研究,我发现可以使用Java SDK访问File
。我的问题是,我是否必须构建自己的UI来显示我获得的文件列表?如果我必须构建我的UI以显示这些文件,我该如何处理文件夹层次结构?
下面是打印有关public class GoogleDriveActivity extends Activity {
private GoogleApiClient mGoogleApiClient;
public com.google.api.services.drive.Drive mService;
public GoogleAccountCredential credential;
public static final int REQUEST_AUTHORIZATION = 3;
public static final int REQUEST_ACCOUNT_PICKER = 4;
private static final String PREF_ACCOUNT_NAME = "accountName";
private static final String[] SCOPES = {DriveScopes.DRIVE_METADATA_READONLY};
final HttpTransport transport = AndroidHttp.newCompatibleTransport();
final JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_drive);
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
credential = GoogleAccountCredential.usingOAuth2(
getApplicationContext(), Arrays.asList(SCOPES))
.setBackOff(new ExponentialBackOff())
.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, "abc.test@gmail.com"));
mService = new com.google.api.services.drive.Drive.Builder(
transport, jsonFactory, credential)
.setApplicationName("My Application")
.build();
}
@Override
public void onResume() {
super.onResume();
refreshResults();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_AUTHORIZATION:
if (resultCode != RESULT_OK) {
chooseAccount();
}
break;
case REQUEST_ACCOUNT_PICKER:
Log.w("gd", "in account picker");
if (resultCode == RESULT_OK && data != null &&
data.getExtras() != null) {
String accountName =
data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null) {
credential.setSelectedAccountName(accountName);
SharedPreferences settings =
getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(PREF_ACCOUNT_NAME, accountName);
editor.commit();
}
} else if (resultCode == RESULT_CANCELED) {
Log.W("gd", "in cancelled");
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
private void chooseAccount() {
startActivityForResult(
credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
private void refreshResults() {
new GoogleDriveAsync(this).execute();
}
public class GoogleDriveAsync extends AsyncTask<Void, Void, Void> {
private GoogleDriveActivity activity;
@Override
protected Void doInBackground(Void... voids) {
try {
getDataFromApi();
} catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
Log.w("gd", "GPS unavailable");
} catch (UserRecoverableAuthIOException userRecoverableException) {
Log.w("gd", "user recoverable");
activity.startActivityForResult(
userRecoverableException.getIntent(),
GoogleDriveActivity.REQUEST_AUTHORIZATION);
} catch (Exception e) {
Log.w("gd", "general exception " + e.getMessage());
}
return null;
}
GoogleDriveAsync(GoogleDriveActivity activity) {
this.activity = activity;
}
/**
* Fetch a list of up to 10 file names and IDs.
*
* @return List of Strings describing files, or an empty list if no files
* found.
* @throws IOException
*/
private List<String> getDataFromApi() throws IOException {
// Get a list of up to 10 files.
List<String> fileInfo = new ArrayList<String>();
FileList result = activity.mService.files().list()
.setMaxResults(10)
.execute();
List<File> files = result.getItems();
if (files != null) {
for (File file : files) {
fileInfo.add(String.format("%s (%s) (%s)\n",
file.getTitle(), file.getId(), file.getDownloadUrl()));
}
}
Log.w("gd", "file info is " + fileInfo.toString());
return fileInfo;
}
}
}
的数据的代码。我已根据metadata实现此代码。
downloadUrl
编辑:请查看我的答案(不是接受的答案)以获取工作样本。用例是:列出用户的所有Google云端硬盘文件,选择后,获取该文件的access_token
和Public Sub GenSQL()
Dim i&, j&, sql$, v, k
k = [d1].Value
v = [d3].CurrentRegion
For j = 1 To UBound(v, 2) Step 2
For i = 1 To UBound(v)
If v(i, j) = k Then
sql = sql & " select @Name = '" & v(1, j) & "', @price = " & v(i, j + 1) & ";"
End If
Next
Next
sql = Mid$(sql, 2)
MsgBox sql
End Sub
。
答案 0 :(得分:3)
要使用基于GDAA的应用程序之外的文件/文件夹,您需要所谓的ResourceID。此ResourceId是一个唯一标识Google云端硬盘对象的字符串(请参阅here)
将DriveId变为ResourceId:
android:layout_gravity="left|bottom"
拥有ResourceId后,您可以构建&#39;下载网址&#39;从它的服务器应用程序。如果您转到drive.google.com,选择文件/文件夹并执行 rightbutton&gt;,则会找到ResourceID字符串。 getLink 。
(看起来像&#39; https://drive.google.com/open?id=0B1mQUW2__I_am_the_resource_id&#39;)
只需取这个字符串&#39; 0B1mQUW2__I_am_the_resource_id &#39;到'TryIt' playground here并将其粘贴到&#39; fileId&#39;字段。
因此,简短的回答是,您不需要RESTful Api来获取您可以在其他地方使用的文件/文件夹标识符。
问题的第二部分&#39; 如果我必须构建我的用户界面以显示这些文件,我如何处理文件夹层次结构?&#39;在[createTree()/ testTree()&#39;中回答(有些)这两个演示的主要活动方法(GDAADemo,RESTDemo)。这些是在GDAA和REST Apis上实现的相同任务,选择主要取决于您的应用程序需要的SCOPE(GDAA仅支持FILE范围,而REST支持FILE和DRIVE范围)
祝你好运