我想传递访问令牌以获取谷歌驱动器中的文件列表。
我做了什么:
将应用注册到Google开发者控制台。
我的客户端ID基于OAuth 2.0
我想做什么:
致电Caught SIGTERM signal!
HttpCall
我想在标题中设置访问令牌,如下所示:
https://www.googleapis.com/drive/v2/files
如何使用DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("https://www.googleapis.com/drive/v2/files");
post.addHeader("Content-Type", "application/json");
post.addHeader("Authorization","Bearer XXXXXXXXXXXXXXXXXXXXXXXXX");
客户端ID
答案 0 :(得分:1)
您应该查看Drive for Android文档。
这使得整个工作变得更加简单。例如
@Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
IntentSender intentSender = Drive.DriveApi
.newOpenFileActivityBuilder()
.setMimeType(new String[] { "text/plain", "text/html" })
.build(getGoogleApiClient());
try {
startIntentSenderForResult(
intentSender, REQUEST_CODE_OPENER, null, 0, 0, 0);
} catch (SendIntentException e) {
Log.w(TAG, "Unable to send intent", e);
}
}
列出文件夹中的所有文件
/**
* An activity that illustrates how to list files in a folder.
*/
public class ListFilesInFolderActivity extends BaseDemoActivity implements
ResultCallback {
private static DriveId sFolderId = DriveId.decodeFromString("DriveId:0B2EEtIjPUdX6MERsWlYxN3J6RU0");
private ListView mResultsListView;
private ResultsAdapter mResultsAdapter;
@Override
public void onConnected(Bundle connectionHint) {
super.onCreate(connectionHint);
setContentView(R.layout.activity_listfiles);
mResultsListView = (ListView) findViewById(R.id.listViewResults);
mResultsAdapter = new ResultsAdapter(this);
mResultsListView.setAdapter(mResultsAdapter);
DriveFolder folder = Drive.DriveApi.getFolder(getGoogleApiClient(), sFolderId);
folder.listChildren(getGoogleApiClient()).setResultCallback(childrenRetrievedCallback);
}
ResultCallback<MetadataBufferResult> childrenRetrievedCallback = new
ResultCallback<MetadataBufferResult>() {
@Override
public void onResult(MetadataBufferResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Problem while retrieving files");
return;
}
mResultsAdapter.clear();
mResultsAdapter.append(result.getMetadataBuffer());
showMessage("Successfully listed files.");
}
}
}
查询文件说“HelloWorld.java”:
Query query = new Query.Builder()
.addFilter(Filters.eq(SearchableField.TITLE, "HelloWorld.java"))
.build();
Drive.DriveApi.query(googleApiClient, query);
答案 1 :(得分:0)