gpt4 book ai didi

android - GoogleAccountCredential setSelectedAccountName 但仍然为空

转载 作者:行者123 更新时间:2023-11-29 16:59:30 26 4
gpt4 key购买 nike

账户名设置后还是空

我看过 this answer ,但我确实在 list 中拥有 GET_ACCOUNTS 权限,并且在运行时受到保护。

以下设置正在 Pixel XL 模拟器 API 25 (Android 7.1.1) 上运行。

list

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

Activity

private void queryAPI() {
if (mCredential.getSelectedAccountName() == null) {
if (checkSelfPermission(Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] {Manifest.permission.GET_ACCOUNTS}, RCP_AUTH_CRED);
return;
}

mCredential.setSelectedAccountName("test@gmail.com");
Log.d(TAG, "queryAPI: account name " + mCredential.getSelectedAccountName());
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case RCP_AUTH_CRED:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
queryAPI();
} else {
Log.d(TAG, "onRequestPermissionsResult: " + Integer.toString(grantResults[0]));
}
break;
}
}

日志

04-18 21:26:47.590 2700-2700/com.package D/MainActivity: queryAPI: account name null

最佳答案

使用 setSelectedAccount() 而不是 setSelectedAccountName()。

 mCredential = GoogleAccountCredential.usingOAuth2(
getApplicationContext(), Arrays.asList(SCOPES))
.setBackOff(new ExponentialBackOff());

// to set accountName manually instead of prompting user to select it
mCredential.setSelectedAccount(new Account("xyz@gmail.com", "com.android.example"));

将您的 gmail 帐户作为第一个参数,将您的包名称作为第二个参数。

关于android - GoogleAccountCredential setSelectedAccountName 但仍然为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43486446/

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