gpt4 book ai didi

android - Google Drive API 迁移到 REST API

转载 作者:行者123 更新时间:2023-12-02 18:24:17 24 4
gpt4 key购买 nike

由于 Google 已弃用 Android API,我正在尝试迁移到 REST API。

我的应用使用 Google 云端硬盘保存用户数据。

用户有两种备份选项(手动和计划)。

用户选择一个帐户并将其存储在应用程序中(电子邮件)。

需要时,应用会使用该帐户连接到 Google 云端硬盘并保存/删除数据。

要选择使用该应用程序的帐户,请使用 AccountPicker。

选择帐户后,应用程序将仅使用该帐户连接到 Google 云端硬盘(请参阅下面的代码)。

我想保留当前机制(用户选择一个帐户,应用程序在需要时使用该帐户连接到 Google 云端硬盘)。

我查看了sample程序和 migration文档,但不知道如何做。

似乎在示例应用程序中,提示需要具有专用 Activity 的帐户,并使用返回的数据登录 Google 云端硬盘(不是我需要的行为)。

我做了一些代码更改,但没有任何效果(我收到错误云端硬盘连接失败 (12500) 12500: 12500: 设置 Google 帐户时出错。)。请参阅下面修改后的代码。

退出代码

GoogleApiClient.Builder builder = new GoogleApiClient.Builder(context)
.addApi(Drive.API)
.setAccountName(accountName)
.addScope(Drive.SCOPE_FILE)
.addScope(Drive.SCOPE_APPFOLDER);
client = builder.build();
client.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnectionSuspended(int cause) {
}

@Override
public void onConnected(Bundle arg0) {
latch.countDown();
}
});
client.registerConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
error = new DriveConnectException();
if (result.hasResolution()) {
if (activity != null) {
try {
result.startResolutionForResult(activity, requestCode);
error = new InResolutionException();
} catch (IntentSender.SendIntentException e) {
}
}
}
latch.countDown();
}
});
client.connect();
try {
latch.await();
} catch (Exception ignored) {
}
if (client.isConnected()) {
// do some work
} else {
// report error
}

修改后的代码

GoogleSignInOptions signInOptions =
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.setAccountName(accountName)
.requestScopes(new Scope(DriveScopes.DRIVE))
.build();
client = GoogleSignIn.getClient(context, signInOptions);
Task<GoogleSignInAccount> task = client.silentSignIn();
if (task.isSuccessful()) {
signInAccount = task.getResult();
} else {
final CountDownLatch latch = new CountDownLatch(1);
task.addOnCompleteListener(new OnCompleteListener<GoogleSignInAccount>() {
@Override
public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
try {
signInAccount = task.getResult(ApiException.class);
} catch (ApiException e) {
// I always ends up here.
}
latch.countDown();
}
});
try {
latch.await();
} catch (Exception ignored) {
}
}

最佳答案

首先您需要登录用户:

    GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(new Scope(DriveScopes.DRIVE_FILE))
.build();
GoogleSignInClient client = GoogleSignIn.getClient(activity, signInOptions);

// The result of the sign-in Intent is handled in onActivityResult
startActivityForResult(client.getSignInIntent(), RC_CODE_SIGN_IN);

在 onActivityResult 中:

GoogleSignIn.getSignedInAccountFromIntent(result)
.addOnSuccessListener(new OnSuccessListener<GoogleSignInAccount>() {
@Override
public void onSuccess(GoogleSignInAccount googleSignInAccount) {
HyperLog.i(TAG, "Signed in as " + googleSignInAccount.getEmail());
mDriveServiceHelper = getDriveServiceHelper(googleSignInAccount);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
HyperLog.e(TAG, "Unable to sign in!", e);
}
});

您可以通过以下方式检索用户登录的最后一个 Google 登录帐户:

GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(getActivity());

要获取 DriveServiceHelper,您可以使用您检索到的帐户:

mDriveServiceHelper = getDriveServiceHelper(account);

“getDriveServiceHelper”方法如下所示:

private DriveServiceHelper getDriveServiceHelper(GoogleSignInAccount googleSignInAccount) {
// Use the authenticated account ot sign in to the Drive service
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
activity, Collections.singleton(DriveScopes.DRIVE_FILE));
credential.setSelectedAccount(googleSignInAccount.getAccount());

Drive googleDriveService = new Drive.Builder(AndroidHttp.newCompatibleTransport(),
new GsonFactory(), credential)
.setApplicationName("COL Reminder")
.build();
return new DriveServiceHelper(googleDriveService);
}

关于android - Google Drive API 迁移到 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53796718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com