如何在Android App中设置Google Drive Credentials?

如何在Android App中设置Google Drive Credentials?,第1张

概述该应用程序在“GoogleAPI控制台”中注册为“已安装的应用程序”–似乎这是Android应用程序的正确设置,不是吗?所以我确实有一个Client-Id而且没有Secret-Id.要说清楚:它不是Web应用程序,也不是GoogleDrive-App–它是一个Android应用程序,用于访问Google云端硬盘云中的其他用户数

该应用程序在“Google Api控制台”中注册为“已安装的应用程序” – 似乎这是Android应用程序的正确设置,不是吗?

所以我确实有一个ClIEnt-ID而且没有Secret-ID.要说清楚:它不是Web应用程序,也不是Google Drive-App–它是一个AndroID应用程序,用于访问Google云端硬盘云中的其他用户数据.

在应用程序中我获取帐户(工作),我确实请求令牌(工作).现在,我想使用该令牌和ClIEnt-ID连接到Google云端硬盘.结果是“401,无效凭证”.这段代码出了什么问题?

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);            }        }    }}

我不得不说这是一堆乱七八糟的东西.网页上有很多页面只显示部分,有很多页面使用弃用,缺失或不同的方法来做同样的事情.在我看来,有两个页面显示了从AndroID应用中从Google云端硬盘获取数据的相同方式.

任何帮助都非常感谢.

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

>必须设置androID:minSdkVersion =“11”作为要求
>不得不使用当前的库: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

这是获取Drive对象的当前部分:

    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) {    }

解决方法:

是的,文档很难流行.

只是改变

new GoogleKeyInitializer(CLIENT_ID)

new GoogleKeyInitializer(SIMPLE_API_ACCESS_KEY)

它应该工作.

您可以在API访问页面的简单API访问部分(API密钥)下的Google APIs Console中找到您的SIMPLE_API_ACCESS_KEY.如果此部分不可用,则必须先在“服务”页面上激活Drive API访问权限.

总结

以上是内存溢出为你收集整理的如何在Android App中设置Google Drive Credentials?全部内容,希望文章能够帮你解决如何在Android App中设置Google Drive Credentials?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1098221.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存