2012-04-20 33 views
2

我正在开发一个多用户Android应用程序,该应用程序向用户提供访问GMaps(查找另一个),聊天等内容。用户应使用Twitter,Facebook,Google+等帐户登录应用程序。除了G + - 应用程序只能通过其所有者帐户访问G + API外,所有帐户都可以正常工作。对于其他帐户,我收到com.google.api.client.googleapis.json.GoogleJsonResponseException:404未找到或“授权错误”。应用程序在API控制台上注册,并使用OAuth2.0身份验证。我使用Google网站的标准身份验证机制。是否可以使用不同的G +帐户登录? 这里是我的代码(安卓V1.6):如何在Android应用上使用Google+帐户提供多用户访问

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

}

我已经搜索在谷歌网站,堆栈溢出,但一无所获。请帮忙。

回答

0

不知道这会帮助,但如果你感到绝望....一些Android的客户端库对2012/4/4 here

新版本,有新鲜的Google+在main()方法中使用一些重新配置的类来访问受保护的资源。 R 1.8中的新版本与您的代码不同,至少在堆栈顶部。IMO在Credential类和PLUS.Builder的新示例中的使用可能会归结为非常多您已拥有的相同实现。如果你不能得到任何其他的工作,你可能想看看新的样本。从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(); 

旧的代码here

+0

非常感谢,我会在这方面努力。 – Darkmike 2012-04-23 11:40:53

+0

更新项目库和访问算法是一个伟大的原因!我终于明白Google+资源访问算法)))但正如我今天看到的,我的错误是项目中使用的测试帐户没有G +配置文件。创建配置文件后,旧版和新版都可以使用。无论如何,谢谢你的信息。 – Darkmike 2012-04-24 15:39:43

相关问题