0

我希望能够创建一个使用 Google 任务的自定义应用程序。我将如何从 Android 应用程序中使用 Google 任务 API?

换句话说,我如何进行 API 调用并使用响应?

我在这里找到了一个很有帮助的教程。

4

1 回答 1

0

cloud.google.com注册,启用Tasks API,并创建 android 授权凭证

添加到您的 AndroidManifest.xml:

<uses-permission android:name="android.permission.GET_ACCOUNTS" android:maxSdkVersion="25"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS" android:maxSdkVersion="22" />

添加到您的 build.gradle 依赖项:

implementation 'com.google.apis:google-api-services-tasks:v1-rev58-1.25.0'
implementation 'com.google.api-client:google-api-client-android:1.28.0'

用于AccountManager.newChooseAccountIntent获取帐户,然后:

String token = AccountManager.get(context)
    .getAuthToken(<android.accounts.Account>, "oauth2:" + TasksScopes.TASKS, new Bundle(), activity, null, null)
    .getResult()
    .getString(AccountManager.KEY_AUTHTOKEN);
GoogleCredential credential = new GoogleCredential().setAccessToken(token);
Tasks service = new Tasks.Builder(new NetHttpTransport(), new JacksonFactory(), credential)
    .setApplicationName("app name")
    .build();

现在您可以使用 Tasks API,例如service.tasklists().list().execute()

于 2019-05-30T21:11:14.330 回答