简体   繁体   中英

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. 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. With other accounts I receive com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found or "authorization error". App is registered on the API Console, and OAuth2.0 authentication used. I use standard authentication mechanism from Google sites. Is it possible to use different G+ accounts to login? Here is my code (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. 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

and there is fresh Google+ sample, using some reconfigured classes in the main() method where they access protected resources. 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. 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

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM