我想在我的Android应用程序中添加一个在用户GoogleDrive上存储文件的功能。应用程序使用Xamarin.Android。如Google文档所述:(https://developers.google.com/drive/api/v3/about-auth#AboutAuthorization)
关于授权协议的
,您的应用程序必须使用OAuth 2.0来授权请求。不支持其他授权协议。如果您的应用程序使用Google登录,授权的某些方面将为您处理。
Google In是可能的,而且应该更容易。但是,我找到的所有Google SingIn示例都以获取用户属性(如电子邮件或姓氏)为结束,但我无法找到如何将接收到的用户属性用于Google或任何其他API授权。例如,来自Google站点:
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
updateUI(account);或者在下面的C#示例中:
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if(requestCode == SING_IN_ACTIVITY_RESULT)
{
GoogleSignInResult res = Auth.GoogleSignInApi.GetSignInResultFromIntent(data);
if(res.IsSuccess)
{
GoogleSignInAccount googleAccount = res.SignInAccount;
}
}
}而Drive服务需要UserCredential对象,这是OAuth2过程的结果:
{ //...
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
// Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});如何将GoogleSignInAccount转换为UserCredential?
发布于 2020-06-08 17:32:05
GoogleSingIn只需登录,您就不能将结果转换为UserCredential,然后使用谷歌驱动器.
解决方案将是寻找特定的google驱动API。
https://stackoverflow.com/questions/62267834
复制相似问题