如何获得谷歌驱动器休息api呼叫的访问令牌?

时间:2015-08-07 12:18:25

标签: android google-drive-android-api

我想传递访问令牌以获取谷歌驱动器中的文件列表。

我做了什么:

  1. 将应用注册到Google开发者控制台。

  2. 我的客户端ID基于OAuth 2.0

  3. 我想做什么:

    1. 致电Caught SIGTERM signal!

    2. HttpCall
    3. 我想在标题中设置访问令牌,如下所示:

      https://www.googleapis.com/drive/v2/files

    4. 如何使用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

    5. 获取授权访问令牌

2 个答案:

答案 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);

有关查询please look at this part of documentation的详细信息。

答案 1 :(得分:0)

如果您使用GDAA中提供的范围进行通话,那么这将是您最好的选择。

否则您可以使用Google Drive Java客户端库。在这种情况下,您可以使用GoogleAccountCredential for Auth,它将包含访问令牌,但您不需要直接使用它。查看示例here