简体   繁体   English

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

[英]How can I provide multiple-user access using Google+ accounts on the Android app

I'm working on a multi-user Android application that provides to its users access to GMaps (find one another), chat and so on. 我正在开发一个多用户Android应用程序,该应用程序向其用户提供对GMaps(彼此查找),聊天等的访问权限。 Users should login to application using their accounts on Twitter, Facebook, Google+ etc. Everything works fine with all accounts except G+ - application can get access to G+ API only with its owner account. 用户应使用其在Twitter,Facebook,Google +等上的帐户登录到应用程序。除G +之外,所有其他帐户都可以正常使用-应用程序只能使用其所有者帐户才能访问G + API。 With other accounts I receive com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found or "authorization error". 使用其他帐户,我会收到com.google.api.client.googleapis.json.GoogleJsonResponseException:找不到404或“授权错误”。 App is registered on the API Console, and OAuth2.0 authentication used. 应用已在API控制台上注册,并使用了OAuth2.0身份验证。 I use standard authentication mechanism from Google sites. 我使用来自Google网站的标准身份验证机制。 Is it possible to use different G+ accounts to login? 是否可以使用其他G +帐户登录? Here is my code (Android v1.6): 这是我的代码(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();
}

} }

I've searched on Google sites, Stack Overflow but found nothing. 我已经在Google网站上搜索了Stack Overflow,但一无所获。 Please help. 请帮忙。

Dont know that this will help but, if you are desperate.... 不知道这会有所帮助,但是,如果您不顾一切的话...

New release of some of the Android client side libs on 4/4/2012 here 2012年4月4日在这里新发布了一些Android客户端库

and there is fresh Google+ sample, using some reconfigured classes in the main() method where they access protected resources. 还有一个新的Google+示例,它在main()方法中使用一些重新配置的类来访问受保护的资源。 The new version in R 1.8 is different than your code , at least at the top of the stack.... IMO the use in the new example of the Credential class and of the PLUS.Builder is probably going to boil down to pretty much the same implementation that you already have. R 1.8中的新版本与您的代码不同,至少在堆栈的顶部。...IMO在Credential类和PLUS.Builder的新示例中的用法可能会归结为您已经拥有的相同实现。 You may want to look at the newer sample if you cannot get anything else to work. 如果您无法使其他任何功能正常工作,则可能需要查看较新的示例。

new code from googlePlus sample in 1.8 GooglePlus示例1.8中的新代码

  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();

older code here 这里的旧代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Google+ API访问Google+中的用户圈 - How To Access a User Circle in Google+ Using Google+ API 如何将多个Google帐户添加到Android应用中,以便用户可以在多个帐户之间切换 - How to add multiple google accounts to an android app so that user can switch between accounts 如何使用OAuth2.0解决socialauth-android(by 3pillarlabs)和Google帐户/ Google + - How to solve socialauth-android (by 3pillarlabs) and Google Accounts/Google+ using OAuth2.0 Android:多用户如何更改每位用户的系统区域设置/语言 - Android: multiple-user how to change system locale/ language per single User 通过Android应用访问Google+照片 - Access Google+ photos from Android app 如何获得椭圆形照片,例如Google+应用程序? - How can I get oval photos like Google+ app? 如何通过G + Android SDK在Google+上的一篇文章中上传多张照片? - How can I upload multiple photos in one post on Google+ from G+ Android SDK? 使用Google+ API登录Android应用 - Using Google+ API for login in Android app 存储访问令牌,Google+集成Android应用 - Storing Access token, Google+ integration Android App 我如何确定用户是否登录,以及如何从google + api注销用户? - How can i determine if the user is signed in or not , and how to sign the user out from google+ api?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM