2

我正在开发一个与我个人帐户中的 Google 电子表格通信的 Android 应用程序。使用 Android Google API 级别 10 和以下库:

android-support-v4.jar;
gdata-client-1.0.jar;
gdata-client-meta-1.0.jar;
gdata-core-1.0.jar;
gdata-spreadsheet-3.0.jar;
gdata-spreadsheet-meta-3.0.jar;
google-api-client-1.12.0-beta.jar;
google-api-client-android-1.12.0-beta.jar;
google-http-client-1.12.0-beta.jar;
google-http-client-android-1.12.0-beta.jar;
google-oauth-client-1.12.0-beta.jar;
gson-2.1.jar;
guava-13.0.1.jar;
jackson-core-asl-1.9.9.jar;
jsr305-1.3.9.jar;
protobuf-java-2.4.1.jar.

要向/从 Google 电子表格发送/接收数据(使用 Google Gdata Spreadsheet 3.0 API),我需要使用以下代码在身份验证阶段返回的对象 SpreadsheetService:

/**
* Authentication to the account  
* @return
* @throws AuthenticationException
*/
public static SpreadsheetService authenticate(final String username, final String password) throws AuthenticationException {
    SpreadsheetService service = new SpreadsheetService("v1");
    service.setProtocolVersion(SpreadsheetService.Versions.V3);
    service.setUserCredentials(email, password);
    return service;
}

但是,此过程需要用户名和密码。我需要在没有明确传递用户名和密码的情况下进行 Google 身份验证。

Android 环境提供了一个 Account 对象,该对象来自存储在设备上的帐户数据。

如何使用 Account 对象进行身份验证并获取 SpreadsheetService 对象?非常感谢。

4

1 回答 1

1

嗨,我知道我回答这个答案为时已晚,但我相信其他人会从中获得帮助,您可以执行以下操作:

    private void chooseAccount() {
            Intent intent = AccountManager.newChooseAccountIntent(null, null,
                    new String[] { "com.google" }, false, null, null, null, null);
            startActivityForResult(intent, ACCOUNT_CODE);
        }

        String accessToken= "";
        String accountName = "";
                @Override
                    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
               if (resultCode == RESULT_OK) {
                        if (requestCode == AUTHORIZATION_CODE) {
                            requestToken();
                        } else if (requestCode == ACCOUNT_CODE) {
                            accountName = data
                                    .getStringExtra(AccountManager.KEY_ACCOUNT_NAME);

                            // invalidate old tokens which might be cached. we want a fresh
                            // one, which is guaranteed to work. Invalidate token based on your needs and the way your code is arranged.
                            invalidateToken();

                            requestToken();
                        }
                    }
                    }

        private void invalidateToken() {
                AccountManager accountManager = AccountManager.get(this);
    if(accessToken!= null && !accessToken.equals(""){
    accountManager.invalidateAuthToken("com.google",
                        accessToken);
    }

            }

        private void requestToken() {
String SCOPE1 = "https://spreadsheets.google.com/feeds";
String SCOPE2 = "https://docs.google.com/feeds";
                Account userAccount = null;

                for (Account account : accountManager.getAccountsByType("com.google")) {
                    if (account.name.equals(accountName)) {
                        userAccount = account;

                        break;
                    }
                }

                accountManager.getAuthToken(userAccount, "oauth2:" + SCOPE1 + " " + SCOPE2, null, this,
                        new OnTokenAcquired(), null);
            }
                private class OnTokenAcquired implements AccountManagerCallback<Bundle> {

                    @Override
                    public void run(AccountManagerFuture<Bundle> result) {
                        try {
                            Bundle bundle = result.getResult();

                            Intent launch = (Intent) bundle.get(AccountManager.KEY_INTENT);
                            if (launch != null) {
                                startActivityForResult(launch, AUTHORIZATION_CODE);
                            } else {
                                accessToken= bundle
                                        .getString(AccountManager.KEY_AUTHTOKEN);
                            }
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                }

private void getSpreadSheetServiceInstance(){
 spreadsheetService = new SpreadsheetService(applicationName);
         spreadsheetService.setProtocolVersion(SpreadsheetService.Versions.V3);
spreadsheetService.setAuthSubToken(accessToken);
}  

获取此实例后,请遵循此处的代码片段:Google SpreadSheets Api。不要忘记在 Google Developer 的控制台注册项目并启用 Drive 和 Google Plus Api,生成 Client 和 Api 密钥,设置同意屏幕。

对于 android studio,可以通过 build.gradle 在 dependencies 部分添加以下库:

compile 'com.google.gdata:core:1.47.1'    
compile 'com.google.android.gms:play-services-drive:7.5.0'
compile 'com.google.android.gms:play-services-plus:7.5.0' 

最后是非常重要的 AndroidManifest 文件:

    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
        <!-- To retrieve the account name (email) as part of sign-in: -->
        <uses-permission android:name="android.permission.GET_ACCOUNTS" />
        <!-- To access Google+ Apis -->
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
        <!-- To retrieve OAuth 2.0 tokens or invalidate tokens to disconnect a user. -->
        <uses-permission android:name="android.permission.USE_CREDENTIALS" />
        <!-- To read Google play services -->
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
于 2015-07-03T08:32:35.853 回答