编写Google Tasks或Drive App的当前步骤是什么?

时间:2012-09-13 08:02:50

标签: android oauth-2.0 google-drive-api

在浏览了很多页面(大多数是矛盾的)后,我无法编写一个与OAuth2连接到谷歌的简单Android应用程序:

我从这个页面开始:

https://developers.google.com/google-apps/tasks/oauth-and-tasks-on-android#account

我确实下载了当前的libs,并且不得不经历许多已弃用或缺失的函数:

  • GoogleAccountManager已弃用
  • 当前的libs中找不到HttpTransport,AndroidHttp,AccessProtectedResource,GoogleAccessProtectedResource,JacksonFactory
  • 通过开发者控制台生成CLIENT_ID
  • SECRET的生成不适用于移动应用程序,但是必须

最新描述是什么?什么是最新的演示?

以防万一 - 这是我到目前为止所做的。 processTokenReceived中的所有方法都不是libs的一部分:

public class ActivityMain extends Activity implements DialogInterface.OnClickListener {

    private static final String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/drive";
    private static final String CLIENT_ID = "xxxxxxxxxxxx.apps.googleusercontent.com";

    private AccountManager accountManager;
    private Account[] accounts;
    private String authName;
    private String authToken;

    @Override
    public void onClick(final DialogInterface dialogInterface, final int item) {

        processAccountSelected(accounts[item]);
    }

    @Override
    public void onCreate(final Bundle bundle) {
        super.onCreate(bundle);

        setContentView(R.layout.activitymain);

        accountManager = AccountManager.get(this);
        accounts = accountManager.getAccountsByType("com.google");

        if (accounts == null || accounts.length == 0) {
            // TODO
        } else if (accounts.length == 1) {
            processAccountSelected(accounts[0]);
        } else if (accounts.length > 1) {
            showDialog(MyConstants.DIALOG_ACCOUNTCHOSER);
        }
    }

    @Override
    protected Dialog onCreateDialog(final int id) {
        switch (id) {
            case MyConstants.DIALOG_ACCOUNTCHOSER:
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

                String[] names = new String[accounts.length];

                for (int i = 0; i < accounts.length; i++) {
                    names[i] = accounts[i].name;
                }

                alertDialogBuilder.setItems(names, this);
                alertDialogBuilder.setTitle("Select a Google account");
                return alertDialogBuilder.create();
        }

        return null;
    }

    private void processAccountSelected(final Account account) {
        if (account != null) {
            authName = account.name.toString();
            if (!Tools.isEmpty(authName)) {
                Toast.makeText(this, authName, Toast.LENGTH_LONG).show();

                accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, null, this,
                        new AccountManagerCallback<Bundle>() {

                            public void run(final AccountManagerFuture<Bundle> future) {
                                try {
                                    authToken = future.getResult().getString(
                                            AccountManager.KEY_AUTHTOKEN);
                                    processTokenReceived();
                                } catch (OperationCanceledException exception) {
                                    // TODO
                                } catch (Exception exception) {
                                    Log.d(this.getClass().getName(), exception.getMessage());
                                }
                            }
                        }, null);
            }
        }
    }

    private void processTokenReceived() {
        if (!Tools.isEmpty(authToken)) {
            HttpTransport transport = AndroidHttp.newCompatibleTransport();
            AccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
                    accessToken);
            Tasks service = new Tasks(transport, accessProtectedResource, new JacksonFactory());
            service.accessKey = CLIENT_ID;
            service.setApplicationName("Google-DriveSample/1.0");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

AccountManagergoogle-api-services library结合使用的最新示例是updated Tasks API exampleGoogleAccountManager例如刚搬到another package

如果您没有使用像Maven这样的构建管理器来解决缺少的依赖关系,那么您当然必须手动包含google-api-java-client libraries,提供HttpTransport等。

对于使用AccountManager,使用API​​控制台中提供的Simple API Access键就足够了。只有Google Play Services可用后,带有证书指纹的部分才会相关。