首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何与GoogleAuthorizationCodeFlow一起使用AccessType是脱机的

如何与GoogleAuthorizationCodeFlow一起使用AccessType是脱机的
EN

Stack Overflow用户
提问于 2012-09-26 04:35:07
回答 1查看 9K关注 0票数 6

我必须实现一个功能来上传一个文件到谷歌驱动器。

快速入门指南所示。

首先,我调用generateLinkAuth来生成链接以获取代码。

然后将执行generateCode以获取令牌。

百事大吉。

代码语言:javascript
复制
    List<String> SCOPES = Arrays.asList("https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/userinfo.email",
            "https://www.googleapis.com/auth/userinfo.profile", DriveScopes.DRIVE);
    public String generateLinkAuth() {
            mHttpTransport = new NetHttpTransport();
            mJsonFactory = new JacksonFactory();

            mFlow = new GoogleAuthorizationCodeFlow.Builder(mHttpTransport, mJsonFactory, CLIENT_ID, CLIENT_SECRET, SCOPES).setAccessType("online").setApprovalPrompt("force").build();


            String url = mFlow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();

            System.out.println("url :" + url);
            return url;
        }

        public void generateCode(String code) {
            new GetCredentialFromCode(mContext, code).execute();
        }


        class GetCredentialFromCode extends AsyncTask<Void, Void, Void> {
            private Context mContext;
            private String code;
            private final ProgressDialog mDialog;

            public GetCredentialFromCode(Context context, String cd) {
                mContext = context;
                mDialog = new ProgressDialog(context);
                mDialog.show();
                mDialog.setTitle("Uploading ...");
                code = cd;
            }

            @Override
            protected Void doInBackground(Void... params) {

                if (mFlow != null) {
                    try {

                        mResponse = mFlow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();

                        mCredential = new GoogleCredential();

                        mCredential.setFromTokenResponse(mResponse);


                        String accessToken = mCredential.getAccessToken();
                        Editor edit = mPrefs.edit();
                        edit.putString(GOOGLE_DRIVE_TOKEN, accessToken);
                        edit.commit();

                        String accessTokenSave = mPrefs.getString(GOOGLE_DRIVE_TOKEN, "NO KEY");
                        Log.e(TAG, "access Token Saved is " + accessTokenSave);

                    } catch (IOException e) {
                        Editor edit = mPrefs.edit();
                        edit.putString(GOOGLE_DRIVE_TOKEN, "NO KEY");
                        e.printStackTrace();
                    }
                }

                String accessTokenSave = mPrefs.getString(GOOGLE_DRIVE_TOKEN, "NO KEY");

                mHttpTransport = new NetHttpTransport();
                mJsonFactory = new JacksonFactory();
                mCredential = new GoogleCredential().setAccessToken(accessTokenSave);

                getUserInfo();

                mDriveService = new Drive.Builder(mHttpTransport, mJsonFactory, mCredential).build();
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                mDialog.dismiss();
                GoogleDriveLogInAct act=  (GoogleDriveLogInAct) mContext;
                act.goBackSettingScreen();
            }

        }

问题是,使用setAccessType("online"),令牌将在1小时内过期,因此我将其更改为setAccessType(“脱机”)。

当我更改它并再次运行我的android应用程序时,当我执行下面的代码时,我会得到一个错误。

代码语言:javascript
复制
                mResponse = mFlow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();

                mCredential = new GoogleCredential();

                mCredential.setFromTokenResponse(mResponse);

当我尝试在控制台上调试时,请执行mCredential.setFromTokenResponse(mResponse)。,将引发例外情况。

代码语言:javascript
复制
java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:278)
    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    at java.lang.Thread.run(Thread.java:856)
 Caused by: java.lang.IllegalArgumentException: Please use the Builder and call setJsonFactory, setTransport and setClientSecrets
    at com.google.common.base.Preconditions.checkArgument(Preconditions.java:88)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.setRefreshToken(GoogleCredential.java:294)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.setRefreshToken(GoogleCredential.java:153)
    at com.google.api.client.auth.oauth2.Credential.setFromTokenResponse(Credential.java:556)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.setFromTokenResponse(GoogleCredential.java:313)
    at com.jp.shotdocs.libs.GoogleDriveLib$GetCredentialFromCode.doInBackground(GoogleDriveLib.java:168)
    at com.jp.shotdocs.libs.GoogleDriveLib$GetCredentialFromCode.doInBackground(GoogleDriveLib.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:264)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    ... 4 more

如何脱机使用accessType?

有什么我必须修改,以使用此模式吗?

EN

回答 1

Stack Overflow用户

发布于 2015-01-26 16:09:55

(在评论中回答。见问题没有答案,但问题在评论中解决(或在聊天中扩展) )

“任择议定书”写道:

已经解决了这个问题。我应该通过使用GoogleCredential而不是new GoogleCredential()来声明new GoogleCredential()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12594608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档