本文介绍了如何安装谷歌驱动器Android应用程序中的凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该应用程序已登记在谷歌API控制台作为一个安装的应用程序 - 看来,这是正确的设置一个Android应用程序,没有

所以,我有一个客户端ID,也没有秘密,标识。要清楚:这是没有网络的应用程序,也没有谷歌驱动器 - 应用程序 - 这是一个Android应用程序访问的谷歌驱动器云其他用户的数据。

在我的应用程序获取帐户(工作),我做的请求令牌(作品)。现在我想连接到谷歌驱动器与令牌和客户端ID。其结果是一个401,无效凭证。有什么不对的code?

 公共类ActivityMain扩展活动实现DialogInterface.OnClickListener {

    // https://developers.google.com/drive/scopes
    私有静态最后弦乐AUTH_TOKEN_TYPE =oauth2如下:https://www.googleapis.com/auth/drive;

    // HTTPS://$c$c.google.com/apis/console/
    私有静态最后弦乐CLIENT_ID =999999999999999.apps.googleusercontent.com;

    私人的AccountManager的AccountManager;
    私人帐户[]帐户;
    私人字符串AUTHNAME;
    私人字符串的authToken;

    @覆盖
    公共无效的onClick(最终DialogInterface dialogInterface,最终诠释项){

        processAccountSelected(帐户[项目]);
    }

    @覆盖
    公共无效的onCreate(最终束束){
        super.onCreate(包);

        的setContentView(R.layout.activitymain);

        的AccountManager = AccountManager.get(本);
        账户= accountManager.getAccountsByType(com.google);

        如果(户口== NULL || accounts.length == 0){
            // 去做
        }否则如果(accounts.length == 1){
            processAccountSelected(帐户[0]);
        }否则如果(accounts.length→1){
            的ShowDialog(MyConstants.DIALOG_ACCOUNTCHOSER);
        }
    }

    @覆盖
    受保护的对话框onCreateDialog(最终诠释的id){
        开关(ID){
            案例MyConstants.DIALOG_ACCOUNTCHOSER:
                AlertDialog.Builder alertDialogBu​​ilder =新AlertDialog.Builder(本);

                的String []的名称=新的String [accounts.length]

                的for(int i = 0; I< accounts.length;我++){
                    名称[I] =帐户[我]。名称;
                }

                alertDialogBu​​ilder.setItems(姓名,这一点);
                alertDialogBu​​ilder.setTitle(选择一个谷歌帐户);
                返回alertDialogBu​​ilder.create();
        }

        返回null;
    }

    私人无效processAccountSelected(决算账户){
        如果(帐户!= NULL){
            AUTHNAME = account.name.toString();
            如果(!Tools.isEmpty(AUTHNAME)){
                Toast.makeText(这一点,AUTHNAME,Toast.LENGTH_LONG).show();

                accountManager.getAuthToken(帐户,AUTH_TOKEN_TYPE,空,这一点,
                        新AccountManagerCallback<包>(){

                            公共无效的run(最终AccountManagerFuture<包>未来){
                                尝试 {
                                    的authToken = future.getResult()的getString(
                                            AccountManager.KEY_AUTHTOKEN);
                                    processTokenReceived();
                                }赶上(OperationCanceledException除外){
                                    // 去做
                                }赶上(例外的例外){
                                    Log.d(this.getClass()的getName(),exception.getMessage());
                                }
                            }
                        }, 空值);
            }
        }
    }

    私人无效processListFiles(最终驱动器驱动器){
        名单<文件>结果=新的ArrayList<文件>();
        Files.List请求= NULL;
        尝试 {
            请求= drive.files()列表()。
        }赶上(IOException异常除外){
        }

        做 {
            尝试 {
                文件列表文件= request.execute();

                result.addAll(files.getItems());
                request.setPageToken(files.getNextPageToken());
            }赶上(IOException异常除外){
                //  - > 401无效的凭证
            }
        }而(request.getPageToken()=空&安培;!&安培; request.getPageToken()长度()大于0);
    }

    私人无效processTokenReceived(){
        如果(!Tools.isEmpty(的authToken)){
            最后HttpTransport运输= AndroidHttp.newCompatibleTransport();
            最后JsonFactory jsonFactory =新GsonFactory();
            GoogleCredential凭证=新GoogleCredential();
            credential.setAccessToken(的authToken);
            驱动器驱动器=新Drive.Builder(运输,jsonFactory,证书)
                    .setApplicationName(的getString(R.string.txt_appname))
                    .setJsonHtt prequestInitializer(新GoogleKeyInitializer(CLIENT_ID))
                    。建立();

            如果(开车!= NULL){
                processListFiles(驱动器);
            }
        }
    }
}
 

我不得不说,这是乱七八糟的满负荷。有这么多页,只有网页显示的部分,有这么多页使用去precated,丢失或不同的方法来这样做。还有,在我看来,没有两页显示相同的方式从内部Android应用程序获取从谷歌驱动器的数据。

任何帮助是非常AP preciated。

编辑:我可以解决它自己。这是不同的变化组合:

  • 必须设置安卓的minSdkVersion =11作为一项要求
  • 只好用这种电流库:谷歌的API客户端-1.11.0-beta.jar,谷歌的API客户端,Android为1.11.0-beta.jar,谷歌API的服务驱动,V2- rev9-1.8.0-beta.jar,谷歌HTTP客户端-1.11.0-beta.jar,谷歌的HTTP客户端,Android为1.11.0-beta.jar,谷歌的HTTP客户端GSON 1.11。 0-beta.jar,谷歌的HTTP客户端jackson2-1.11.0-beta.jar,谷歌的OAuth客户端-1.11.0-beta.jar,GSON-2.1.jar,番石榴11.0.1.jar,杰克逊核心2.0.5.jar,jsr305-1.3.9.jar

这是当前部分获得驱动对象:

  GoogleCredential凭证=新GoogleCredential();
    credential.setAccessToken(的authToken);

    HttpTransport运输= AndroidHttp.newCompatibleTransport();

    JsonFactory jsonFactory =新AndroidJsonFactory();

    驱动器=新Drive.Builder(运输,jsonFactory,证书)
            .setApplicationName(的getString(R.string.txt_appname))
            .setJsonHtt prequestInitializer(
                    新GoogleKeyInitializer(APIKEY_SIMPLE))
            。建立();
    如果(开车!= NULL){
    }
 

解决方案

是啊,该文档是非常难赶上。​​

只要修改

 新GoogleKeyInitializer(CLIENT_ID)
 

 新GoogleKeyInitializer(SIMPLE_API_ACCESS_KEY)
 

和它应该工作。

您可以找到你在谷歌的的在简单API访问 API访问栏目页( API密钥 )。如果这部分不可用,你必须首先激活的服务驱动的API访问页。

The app was registered in the "Google API console" as an "installed application" - seems that this is the right setting for an Android app, no?

So I do have a Client-Id and no Secret-Id. To make it clear: It's no Web-app and no Google Drive-App - it's an Android App accessing other users data in the Google Drive cloud.

Within the app I fetch the account (works) and I do request a token (works). Now I want to connect to Google Drive with that token and the Client-Id. The result is a "401, invalid credential". What's wrong with this code?

public class ActivityMain extends Activity implements DialogInterface.OnClickListener {

    // https://developers.google.com/drive/scopes
    private static final String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/drive";

    // https://code.google.com/apis/console/
    private static final String CLIENT_ID = "999999999999999.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 processListFiles(final Drive drive) {
        List<File> result = new ArrayList<File>();
        Files.List request = null;
        try {
            request = drive.files().list();
        } catch (IOException exception) {
        }

        do {
            try {
                FileList files = request.execute();

                result.addAll(files.getItems());
                request.setPageToken(files.getNextPageToken());
            } catch (IOException exception) {
                // --> 401 invalid credentials
            }
        } while (request.getPageToken() != null && request.getPageToken().length() > 0);
    }

    private void processTokenReceived() {
        if (!Tools.isEmpty(authToken)) {
            final HttpTransport transport = AndroidHttp.newCompatibleTransport();
            final JsonFactory jsonFactory = new GsonFactory();
            GoogleCredential credential = new GoogleCredential();
            credential.setAccessToken(authToken);
            Drive drive = new Drive.Builder(transport, jsonFactory, credential)
                    .setApplicationName(getString(R.string.txt_appname))
                    .setJsonHttpRequestInitializer(new GoogleKeyInitializer(CLIENT_ID))
                    .build();

            if (drive != null) {
                processListFiles(drive);
            }
        }
    }
}

I have to say that this is a full load of mess. There are so many pages in the web showing parts only and there are so many pages using deprecated, missing or different methods to do the same. There are, in my opinion, not two pages showing the same way to get data from Google Drive from within an Android app.

Any help is highly appreciated.

EDIT: I could solve it myself. It was a combination of different changes:

  • Had to set android:minSdkVersion="11" as a requirement
  • Had to use this current libraries: google-api-client-1.11.0-beta.jar, google-api-client-android-1.11.0-beta.jar, google-api-services-drive-v2-rev9-1.8.0-beta.jar, google-http-client-1.11.0-beta.jar, google-http-client-android-1.11.0-beta.jar, google-http-client-gson-1.11.0-beta.jar, google-http-client-jackson2-1.11.0-beta.jar, google-oauth-client-1.11.0-beta.jar, gson-2.1.jar, guava-11.0.1.jar, jackson-core-2.0.5.jar, jsr305-1.3.9.jar

This is the current part to get the Drive Object:

    GoogleCredential credential = new GoogleCredential();
    credential.setAccessToken(authToken);

    HttpTransport transport = AndroidHttp.newCompatibleTransport();

    JsonFactory jsonFactory = new AndroidJsonFactory();

    drive = new Drive.Builder(transport, jsonFactory, credential)
            .setApplicationName(getString(R.string.txt_appname))
            .setJsonHttpRequestInitializer(
                    new GoogleKeyInitializer(APIKEY_SIMPLE))
            .build();
    if (drive != null) {
    }
解决方案

Yeah, the documentation is quite hard to catch on.

Just change

new GoogleKeyInitializer(CLIENT_ID)

to

new GoogleKeyInitializer(SIMPLE_API_ACCESS_KEY)

and it should work.

You can find your SIMPLE_API_ACCESS_KEY in Google's APIs Console under the Simple API Access section of the API Access page (the API key). If this section is not available, you have to first activate Drive API access on the Services page.

这篇关于如何安装谷歌驱动器Android应用程序中的凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 01:16