gpt4 book ai didi

Android Google+ 无法获取授权码

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:50 26 4
gpt4 key购买 nike

我正在尝试根据这篇文章获取 Google+ 授权码:Server-side access for your app .但是,每当我运行我的应用程序并尝试获取代码时,我都会在 LogCat 中得到以下内容:

07-23 23:42:31.760: E/getAccessToken()(11114): [ERROR] GoogleAuthException: com.google.android.gms.auth.GoogleAuthException: Unknown

我浏览了很多东西,据我所知,我已经在下面的代码中正确设置了所有内容。为什么它没有获得授权 token 有什么想法吗?

仅供引用,我在 API 控制台中设置了一个项目,其中为 oauth 配置了应用程序和 Web 应用程序,并且我正在使用服务器(而非应用程序)ID 作为范围字符串。我也遵循了上面链接中的示例,该应用程序运行良好并允许您登录/注销,但我就是无法获得授权码。

private String getAccessToken() {
String scopesString = Scopes.PLUS_LOGIN + " " + Scopes.PLUS_PROFILE;
String scope = "oauth2:server:client_id:" + SERVER_CLIENT_ID + ":api_scope:" + scopesString;
Bundle appActivities = new Bundle();
appActivities.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,
"http://schemas.google.com/AddActivity");
String code = null;
try {
code = GoogleAuthUtil.getToken(
this, // Context context
mPlusClient.getAccountName(), // String accountName
scopes, // String scope
appActivities // Bundle bundle
);
} catch (IOException transientEx) {
// network or server error, the call is expected to succeed if you try again later.
// Don't attempt to call again immediately - the request is likely to
// fail, you'll hit quotas or back-off.
Log.e("getAccessToken()", "[ERROR] IOException: " + transientEx);
return null;
} catch (UserRecoverableAuthException e) {
// Recover
Log.e("getAccessToken()", "[ERROR] UserRecoverableAuthException: " + e);
code = null;
} catch (GoogleAuthException authEx) {
// Failure. The call is not expected to ever succeed so it should not be
// retried.
Log.e("getAccessToken()", "[ERROR] GoogleAuthException: " + authEx);
return null;
} catch (Exception e) {
Log.e("getAccessToken()", "[ERROR] Exception: " + e);
throw new RuntimeException(e);
}
return code;
}

最佳答案

试试这个,

在你的 onCreate() 中写,

new GetTokenAsync.execute();

然后编写 AsyncTask 以获取访问 token ,

public class GetTokenAsync extends AsyncTask<String, String, String>
{

@Override
protected void onPreExecute()
{

super.onPreExecute();
plusUtilities.ShowProgressDialog("Getting Token...");

}

@Override
protected String doInBackground(String... params)
{
String scope = "oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_PROFILE;
try
{
// We can retrieve the token to check via
// tokeninfo or to pass to a service-side
// application.
String token = GoogleAuthUtil.getToken(getParent(), mPlusClient.getAccountName(), scope);
Log.i("OAUTH Token:", token);
Log.i("Scope:", "" + scope);
Log.i("GOOGLE_ACCOUNT_TYPE", "" + GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);

Person currentperson = mPlusClient.getCurrentPerson();
Log.i("person", currentperson.toString());

}
catch (UserRecoverableAuthException e)
{
Log.e("UserRecoverableAuthException", "UserRecoverableAuthException");
e.printStackTrace();
}
catch (IOException e)
{
Log.e("IOException", "IOException");
e.printStackTrace();
}
catch (GoogleAuthException e)
{
Log.e("GoogleAuthException", "GoogleAuthException");
e.printStackTrace();
}
catch (Exception e)
{
Log.e("Exception", "Exception");
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String result)
{

super.onPostExecute(result);
plusUtilities.DissmissProgressDialog();
}

}

关于Android Google+ 无法获取授权码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17827256/

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