gpt4 book ai didi

android - 如何在 Android 应用程序上使用 Google+ 帐户提供多用户访问权限

转载 作者:行者123 更新时间:2023-11-29 13:59:23 25 4
gpt4 key购买 nike

我正在开发一个多用户 Android 应用程序,该应用程序为其用户提供 GMaps(查找彼此)、聊天等功能的访问权限。用户应该使用他们在 Twitter、Facebook、Google+ 等上的帐户登录到应用程序。除了 G+ 之外,所有帐户都可以正常工作 - 应用程序只能使用其所有者帐户访问 G+ API。对于其他帐户,我收到 com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found 或“授权错误”。应用程序在 API 控制台注册,并使用 OAuth2.0 身份验证。我使用来自 Google 站点的标准身份验证机制。是否可以使用不同的 G+ 帐户登录?这是我的代码(Android v1.6):

public class GooglePlusActivity extends Activity {

public static final String LOG_TAG = GooglePlusActivity.class.getSimpleName();
public static final String EXTRA_FIRSTNAME = "firstname";
public static final String EXTRA_LASTNAME = "lastname";
public static final String EXTRA_NICKNAME = "nickname";
public static final String EXTRA_SEX = "sex";
public static final String EXTRA_AVATAR = "avatar";
public static final String EXTRA_ID_SOCNET = "id_socnet";

private ApplicationSettings mSettings;
private Person mProfile;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSettings = ((TomskApplication)getApplication()).getSettings();
signIn();
}

private void signIn() {
WebView webView = new WebView(this);
setContentView(webView);
webView.getSettings().setJavaScriptEnabled(false);
String googleAuthorizationRequestUrl = new GoogleAuthorizationRequestUrl(
mSettings.getGPID(), mSettings.getGPRedirectURI(),
mSettings.getGPScope()).build();
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if (url.startsWith(mSettings.getGPRedirectURI())) {
try {
Intent res_intent = new Intent();
if (url.indexOf("code=") != -1) {
String code = url.substring(mSettings
.getGPRedirectURI().length() + 7, url
.length());

AccessTokenResponse token = new GoogleAuthorizationCodeGrant(
new NetHttpTransport(),
new JacksonFactory(), mSettings.getGPID(),
mSettings.getGPSecret(), code, mSettings
.getGPRedirectURI()).execute();

mSettings.setGPToken(token);

// Loading user data
retrieveProfile();
if (mProfile == null) {retrieveProfile();}
res_intent.putExtra(EXTRA_FIRSTNAME, mProfile
.getName().getGivenName());
res_intent.putExtra(EXTRA_LASTNAME, mProfile
.getName().getFamilyName());
res_intent.putExtra(EXTRA_NICKNAME,
mProfile.getNickname());
res_intent.putExtra(EXTRA_SEX, mProfile.getGender());
res_intent.putExtra(EXTRA_AVATAR, mProfile
.getImage().getUrl());
res_intent.putExtra(EXTRA_ID_SOCNET, mProfile.getId());
setResult(Activity.RESULT_OK, res_intent);
view.setVisibility(View.INVISIBLE);
finish();
} else if (url.indexOf("error=") != -1) {
view.setVisibility(View.INVISIBLE);
setResult(Activity.RESULT_CANCELED);
finish();
}

} catch (IOException e) {
Log.d(LOG_TAG, e.toString());
}
return true;
} else {
return false;
}
}

});
webView.loadUrl(googleAuthorizationRequestUrl);
}

/**
* Retrieve user profile
*/
private void retrieveProfile() throws IOException {
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = new NetHttpTransport();

AccessTokenResponse token = mSettings.getGPToken();

GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
token.accessToken, transport, jsonFactory,
mSettings.getGPID(), mSettings.getGPSecret(),
token.refreshToken);

Builder b = Plus.builder(transport, jsonFactory)
.setApplicationName("MyApp/1.0");
b.setHttpRequestInitializer(accessProtectedResource);
Plus plus = b.build();
mProfile = plus.people().get("me").execute();
}

我在 Google 网站和 Stack Overflow 上进行了搜索,但一无所获。请帮忙。

最佳答案

不知道这会有所帮助,但是,如果你绝望的话......

2012 年 4 月 4 日一些 Android 客户端库的新版本 here

还有新的 Google+ 示例,在访问 protected 资源的 main() 方法中使用了一些重新配置的类。 R 1.8 中的新版本与您的代码不同,至少在堆栈的顶部……IMO 在 Credential 类和 PLUS.Builder 的新示例中的使用可能会归结为几乎您已经拥有的相同实现。如果您无法让其他任何东西工作,您可能想要查看较新的样本。

1.8 中来自 googlePlus 示例的新代码

  public static void main(String[] args) {
try {
try {
// authorization
Credential credential = OAuth2Native.authorize(
HTTP_TRANSPORT, JSON_FACTORY, new LocalServerReceiver(),
Arrays.asList(PlusScopes.PLUS_ME));
// set up global Plus instance
plus = Plus.builder(HTTP_TRANSPORT, JSON_FACTORY)
.setApplicationName("Google-PlusSample/1.0").setHttpRequestInitializer(credential)
.build();

旧代码 here

关于android - 如何在 Android 应用程序上使用 Google+ 帐户提供多用户访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10246759/

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