本文介绍了如何从GoogleApiClient获取OAuth令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个Android应用程序,该应用程序在Google云端硬盘中创建了一个电子表格,然后向其中添加了一些数据.我可以使用Google Drive Android API成功创建电子表格,其中涉及创建GoogleApiClient和使用实现GoogleApiClient.ConnectionCallbacks的类.

I am trying to build an Android application that created a spreadsheet in Google Drive and then adds some data to it. I can successfuly create the spreadsheet using the Google Drive Android API which involves creating a GoogleApiClient and using a class that implements GoogleApiClient.ConnectionCallbacks.

启动应用程序时,我会创建一个新的GoogleApiClient,并提示您选择一个帐户.在onConnected回调中,我可以将GoogleApiClient传递给Drive.DriveApi以查询和创建文件和文件夹.

When I start the application I make a new GoogleApiClient and am prompted to select an account. In the onConnected callback I can new pass my GoogleApiClient to Drive.DriveApi to query and create files and folders.

但是,要将数据添加到Google工作表中,我需要使用GoogleSheets API.据我所知,它没有Android API,但确实有gdata API.为了使用它,我需要一个OAuth令牌.

However, to add data to a Google sheet I need to use the GoogleSheets API. This does not have an Android API as far as I can tell but it does have a gdata API. In order to use it however I need an OAuth token.

反正我可以从GoogleApiClient对象获得OAuth令牌吗?

Is there anyway I can get an OAuth token from the GoogleApiClient object?

推荐答案

是的,您只能使用

    GoogleAccountCredential mCredential;
String[] SCOPES = {"https://www.googleapis.com/auth/userinfo.profile"};

     mCredential = GoogleAccountCredential.usingOAuth2(mContext, Arrays.asList(SCOPES))
                    .setBackOff(new ExponentialBackOff())
                    .setSelectedAccountName(ACC_NAME);

        GoogleAuthUtil.getToken(mContext, mCredential.getSelectedAccountName(),SCOPE)

GoogleAuthUtil.getToken

这篇关于如何从GoogleApiClient获取OAuth令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 08:01