如何在共享首选项中存储来自OAuth2的凭据 [英] How store credential from OAuth2 in shared preferences

查看:55
本文介绍了如何在共享首选项中存储来自OAuth2的凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些与Google相关的活动,我只想连接一次Google并在整个应用程序中共享证书.我有一个通过OAuth2向Google进行身份验证的活动.它返回一个凭证,如下所示:

I have several activities that connect to Google, I only want to connect to Google once and share the credential throughout the app. I have an activity that authenticates to Google via OAuth2. It returns a credential as follows:

private GoogleAccountCredential credential;

...

credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);

是否可以将其放入共享首选项"中.我已经尝试了所有明显的类型,例如可包裹的等,并且发现凭证可以放入共享首选项",但是找不到将其转换回GoogleAccountCredential的吸气剂.我什至尝试将其设为对象,但是getter再次将其转换回GoogleAccountCredential类型的问题.

Is there a way to put it into Shared Preferences. I've tried all the obvious types, i.e. parcelable, etc. and I've found the credential can be put into Shared Preferences, but can't find a getter to convert it back to GoogleAccountCredential. I had even tried making it an object, but again issues with the getter converting it back to the GoogleAccountCredential type.

如果共享首选项"不起作用,还有其他选择吗?

If Share Preference isn't capable, it there an alternative?

提前感谢您的时间和帮助.

Thanks in advance for you time and help.

推荐答案

您可能想在Android中使用AccountManager API.帐户管理器能够存储OAuth令牌,并且可以为Android中的所有Google Apps进行这项工作.您可以在此处关注博客.

You might want to use the AccountManager API in Android. Account Manager is capable of storing the OAuth token and does the job for all Google Apps in Android. You may follow the blog here.

这篇关于如何在共享首选项中存储来自OAuth2的凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆