java–Google Drive SDK例外

java–Google Drive SDK例外,第1张

概述我试图运行以下代码(主要从StephenWylie开始):packagecom.googledrive.googledriveapp;//ForGoogleDrive/PlayServices//Version1.1-Addednewcomments&removeddeadcode//StephenWylie-10/20/2012importjava.io.IOException;importjava.util.ArrayLis

我试图运行以下代码(主要从Stephen Wylie开始):

package com.Googledrive.Googledriveapp;// For Google Drive / Play Services// Version 1.1 - Added new comments & removed dead code// Stephen WylIE - 10/20/2012import java.io.IOException;import java.util.ArrayList;import androID.accounts.Account;import androID.accounts.AccountManager;import androID.app.Activity;import androID.app.ProgressDialog;import androID.content.Context;import androID.content.DialogInterface;import androID.content.DialogInterface.OnClickListener;import androID.content.Intent;import androID.os.AsyncTask;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;import com.Google.androID.gms.auth.GoogleAuthException;import com.Google.androID.gms.auth.GoogleAuthUtil;import com.Google.androID.gms.auth.UserRecoverableAuthException;import com.Google.androID.gms.common.AccountPicker;import com.Google.API.clIEnt.auth.oauth2.BearerToken;import com.Google.API.clIEnt.auth.oauth2.Credential;import com.Google.API.clIEnt.extensions.androID2.AndroIDhttp;import com.Google.API.clIEnt.GoogleAPIs.extensions.androID2.auth.GoogleAccountManager;import com.Google.API.clIEnt.http.httpRequestFactory;import com.Google.API.clIEnt.http.httpTransport;import com.Google.API.clIEnt.http.Json.JsonhttpRequest;import com.Google.API.clIEnt.http.Json.JsonhttpRequestinitializer;import com.Google.API.clIEnt.Json.jackson.JacksonFactory;import com.Google.API.services.drive.Drive;import com.Google.API.services.drive.Drive.Apps.List;import com.Google.API.services.drive.Drive.files;import com.Google.API.services.drive.DriveRequest;import com.Google.API.services.drive.DriveScopes;import com.Google.API.services.drive.model.file;import com.Google.API.services.drive.model.fileList;public class MainActivity extends Activity {    private static final int CHOOSE_ACCOUNT=0;    private static String accountname;    private static int REQUEST_TOKEN=0;    private button btn_drive;    private Context ctx = this;    private Activity a = this;    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // set up the GUI layout        setContentVIEw(R.layout.activity_main);        // set the variables to access the GUI controls        btn_drive = (button) findVIEwByID(R.ID.btn_drive);            btn_drive.setonClickListener(new VIEw.OnClickListener() {            public voID onClick(VIEw v) {                chooseAccount();            }            });    }    public voID chooseAccount() {        Intent intent = AccountPicker.newChooseAccountIntent(null, null, new String[]{"com.Google"}, false, null, null, null, null);        startActivityForResult(intent, CHOOSE_ACCOUNT);    }    // Fetch the access token asynchronously.    voID getAndUseAuthTokenInAsyncTask(Account account) {        AsyncTask<Account, String, String> task = new AsyncTask<Account, String, String>() {            ProgressDialog progressDlg;            AsyncTask<Account, String, String> me = this;            @OverrIDe            protected voID onPreExecute() {                progressDlg = new ProgressDialog(ctx, ProgressDialog.STYLE_SPINNER);                progressDlg.setMax(100);                progressDlg.setTitle("ValIDating...");                progressDlg.setMessage("Verifying the login data you entered...\n\nThis action will time out after 10 seconds.");                progressDlg.setCancelable(false);                progressDlg.setIndeterminate(false);                progressDlg.setonCancelListener(new androID.content.DialogInterface.OnCancelListener() {                    public voID onCancel(DialogInterface d) {                        progressDlg.dismiss();                        me.cancel(true);                    }                });                progressDlg.show();            }            @OverrIDe            protected String doInBackground(Account... params) {                return getAccesstoken(params[0]);            }            @OverrIDe            protected voID onPostExecute(String s) {                if (s == null) {                    // Wait for the extra intent                } else {                    accountname = s;                    getDrivefiles();                }                progressDlg.dismiss();            }        };        task.execute(account);    }    /**     * Fetches the token from a particular Google account chosen by the user.  DO NOT RUN THIS DIRECTLY.  It must be run asynchronously insIDe an AsyncTask.     * @param activity     * @param account     * @return     */    private String getAccesstoken(Account account) {        try {            return GoogleAuthUtil.getToken(ctx, account.name, "oauth2:" + DriveScopes.DRIVE);  // important: DriveScopes must be changed depending on what level of access you want        } catch (UserRecoverableAuthException e) {            // Start the Approval Screen intent, if not run from an Activity, add the Intent.FLAG_ACTIVITY_NEW_TASK flag.            a.startActivityForResult(e.getIntent(), REQUEST_TOKEN);            e.printstacktrace();            return null;        } catch (GoogleAuthException e) {            e.printstacktrace();            return null;        } catch (IOException e) {            e.printstacktrace();            return null;        }    }    private Drive getDriveService() {        httpTransport ht = AndroIDhttp.newCompatibleTransport();             // Makes a transport compatible with both AndroID 2.2- and 2.3+        JacksonFactory jf = new JacksonFactory();                            // You need a JsON parser to help you out with the API response        Credential credential = new Credential(BearerToken.authorizationheaderAccessMethod()).setAccesstoken(accountname);        httpRequestFactory rf = ht.createRequestFactory(credential);        Drive.Builder b = new Drive.Builder(ht, jf, null);        b.setJsonhttpRequestinitializer(new JsonhttpRequestinitializer() {            @OverrIDe            public voID initialize(JsonhttpRequest request) throws IOException {                DriveRequest driveRequest = (DriveRequest) request;                driveRequest.setPrettyPrint(true);                driveRequest.setoauthToken(accountname);            }        });        return b.build();    }    /**     * Obtains a List of all files on the signed-in user's Google Drive account.     */    private voID getDrivefiles() {        Drive service = getDriveService();        Log.d("SiteTrack", "FUNCTION getDrivefiles()");        files.List request;        try {            request = service.files().List(); // .setQ("mimeType=\"text/plain\"");        } catch (IOException e) {            e.printstacktrace();            return;        }        do {            fileList files;            try {                System.out.println("got here");                Log.d("SiteTrack", request.toString());                **files = request.execute();**            } catch (IOException e) {                e.printstacktrace();                Log.d("SiteTrack", "Exception");                return;            }            ArrayList<file> fileList = (ArrayList<file>) files.getItems();            Log.d("SiteTrack", "files found: " + files.getItems().size());            for (file f : fileList) {                String fileID = f.getID();                String Title = f.getTitle();                Log.d("SiteTrack", "file " + fileID + ": " + Title);            }            request.setPagetoken(files.getNextPagetoken());        } while (request.getPagetoken() != null && request.getPagetoken().length() >= 0);    }    protected voID onActivityResult(final int requestCode, final int resultCode, final Intent data) {        if (requestCode == CHOOSE_ACCOUNT && resultCode == RESulT_OK) {            accountname = data.getStringExtra(AccountManager.KEY_ACCOUNT_name);            GoogleAccountManager gam = new GoogleAccountManager(this);            getAndUseAuthTokenInAsyncTask(gam.getAccountByname(accountname));            Log.d("SiteTrack", "CHOOSE_ACCOUNT");        } else if (requestCode == REQUEST_TOKEN && resultCode == RESulT_OK) {            accountname = data.getStringExtra(AccountManager.KEY_ACCOUNT_name);            Log.d("SiteTrack", "REQUEST_TOKEN");        }    }   }

但是,我得到以下异常:

11-19 16:35:27.758: W/System.err(23287): com.Google.API.clIEnt.GoogleAPIs.Json.GoogleJsonResponseException: 403 ForbIDden11-19 16:35:27.758: W/System.err(23287): {11-19 16:35:27.758: W/System.err(23287):   "code" : 403,11-19 16:35:27.758: W/System.err(23287):   "errors" : [ {11-19 16:35:27.758: W/System.err(23287):     "domain" : "usagelimits",11-19 16:35:27.762: W/System.err(23287):     "message" : "Access Not Configured",11-19 16:35:27.762: W/System.err(23287):     "reason" : "accessNotConfigured"11-19 16:35:27.762: W/System.err(23287):   } ],11-19 16:35:27.762: W/System.err(23287):   "message" : "Access Not Configured"11-19 16:35:27.762: W/System.err(23287): }11-19 16:35:27.762: W/System.err(23287):    at com.Google.API.clIEnt.GoogleAPIs.services.GoogleClIEnt.executeUnparsed(GoogleClIEnt.java:237)11-19 16:35:27.762: W/System.err(23287):    at com.Google.API.clIEnt.http.Json.JsonhttpRequest.executeUnparsed(JsonhttpRequest.java:207)11-19 16:35:27.762: W/System.err(23287):    at com.Google.API.services.drive.Drive$files$List.execute(Drive.java:1071)11-19 16:35:27.762: W/System.err(23287):    at com.Googledrive.Googledriveapp.MainActivity.getDrivefiles(MainActivity.java:173)11-19 16:35:27.762: W/System.err(23287):    at com.Googledrive.Googledriveapp.MainActivity.access(MainActivity.java:156)11-19 16:35:27.762: W/System.err(23287):    at com.Googledrive.Googledriveapp.MainActivity.onPostExecute(MainActivity.java:104)11-19 16:35:27.765: W/System.err(23287):    at com.Googledrive.Googledriveapp.MainActivity.onPostExecute(MainActivity.java:1)11-19 16:35:27.765: W/System.err(23287):    at androID.os.AsyncTask.finish(AsyncTask.java:417)11-19 16:35:27.765: W/System.err(23287):    at androID.os.AsyncTask.access0(AsyncTask.java:127)11-19 16:35:27.765: W/System.err(23287):    at androID.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)11-19 16:35:27.765: W/System.err(23287):    at androID.os.Handler.dispatchMessage(Handler.java:99)11-19 16:35:27.765: W/System.err(23287):    at androID.os.Looper.loop(Looper.java:123)11-19 16:35:27.765: W/System.err(23287):    at androID.app.ActivityThread.main(ActivityThread.java:4627)11-19 16:35:27.765: W/System.err(23287):    at java.lang.reflect.Method.invokeNative(Native Method)11-19 16:35:27.769: W/System.err(23287):    at java.lang.reflect.Method.invoke(Method.java:521)11-19 16:35:27.769: W/System.err(23287):    at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)11-19 16:35:27.769: W/System.err(23287):    at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:616)11-19 16:35:27.769: W/System.err(23287):    at dalvik.system.NativeStart.main(Native Method)

跟踪行文件= request.execute();在上面的代码中(我用星号标记).我在Google Api控制台中启用了Drive SDK和Drive API.以下是我的Drive SDK设置的几个快照:

对于未图示的客户端ID部分,我只是从API访问部分粘贴了“已安装应用程序的客户端ID”(我还尝试了“Drive SDK的客户端ID”).谁知道问题是什么?

解决方法:

编辑:对不起我刚刚意识到我们可以使用deBUG.keystore在调试模式下签署apk.
因此,最重要的是在Google Api控制台中插入正确的SHA1密钥. (使用Stephen WylIE在他的Google帖子中提到的JRE7工具生成)

另请参阅(developer.androID.com/tools/publishing/app-signing.HTML#deBUGmode)以获取密钥库密码

经过一些试验和错误,我终于能够在我的谷歌硬盘中列出文件
虽然我不确定这是否是最正确的答案,但至少我现在能够列出文件

首先,请参阅Stephen WylIE更新的Google帖子
https://plus.google.com/u/0/114042449736049687152/posts/CD3L8zcJg5Z

使用JRE7工具使用.keystore生成SHA1密钥

我正在使用我一直在谷歌Play商店使用的.keystore文件
(如果您没有任何密钥库,可以转到项目,单击“导出”,然后它将请求您创建.keystore文件)

使用生成的SHA1密钥,转到您的Google Api控制台(最好从新手开始,删除当前项目并开始新项目)

-enable Drive API

-enable Drive SDK

– 访问API ACCESS

– 创建客户端ID

-important在这里,选择Installed application& AndroID的

-ALSO重要,键入生成的SHA1密钥

-key在您的eclipse项目中使用的CORRECT包名(com.example.xxx)中

– 转到Drive SDK选项卡 – 上载图标

-important,从API Access选项卡键入您的CLIENT ID(已安装应用程序的客户端ID)

– 插入Stephen WylIE帖子中提到的3个范围,[/ userinfo.email,/ userinfo.profile,/ auth / drive]

– 插入一个URL

– 获得多个文件支持

同样,请确保代码中的软件包名称与您在Google Api控制台中插入的软件包名称相同

最后,在eclipse中,使用刚刚创建的.keystore文件导出项目

将导出的APK文件放入手机中,安装并试用.

检查您的LogCat以显示Google云端硬盘中列出的文件

我成功地用这种方式.

编辑:如果您使用deBUG.keystore生成SHA1密钥,则可以跳过“导出”部分.只需调试您的应用程序即可. Eclipse将使用deBUG.keystore自动签署apk.

编辑:下次代码准备就绪时,您需要使用真正的.keystore生成一个新的SHA1密钥,然后输入到Google Api控制台.

编辑2:确保您的清单包含这些

<uses-permission androID:name="androID.permission.INTERNET" /><uses-permission androID:name="androID.permission.GET_ACCOUNTS" /><uses-permission androID:name="androID.permission.USE_CREDENTIALS" /><uses-permission androID:name="androID.permission.MANAGE_ACCOUNTS" />
总结

以上是内存溢出为你收集整理的java – Google Drive SDK例外全部内容,希望文章能够帮你解决java – Google Drive SDK例外所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存