GoogleAuthUtil.getToken导致Android应用崩溃

时间:2013-11-18 21:16:23

标签: java android authentication android-asynctask oauth-2.0

我正在尝试使用GoogleAuthUtil.getToken生成可用于登录其他服务的令牌。当我尝试使用此方法时,我的应用程序“挂起”并停止响应。最终我的手机会发出一些IO错误消息。我收不到服务器的结果。

电子邮件地址是我的电子邮件地址,格式为myname@gmail.com,我已经使用了我的范围的服务器地址,但是现在我只是想使用最简单的字符串来获得我能够得到的结果开始使用。

public  class GetTokens extends AsyncTask<String,Void,String>{
 Context c;
 String TAG = "wnycmap issues";

public GetTokens(Context c) {
    this.c=c;
}

@Override
protected String doInBackground(String...mEmail) {
    String token = "null";
    String mScope = "oauth2:https://www.googleapis.com/auth/plus.me";
    String email = mEmail[0];
    System.out.println(c+email+mScope);
    try {
        token = GoogleAuthUtil.getToken(
                c,
                email,
                mScope);

        System.out.println(token);
    } catch (IOException transientEx) {
        // Network or server error, try later
        Log.e(TAG, transientEx.toString());
    } catch (UserRecoverableAuthException e) {
        // Recover (with e.getIntent())
        Log.e(TAG, e.toString());
        // Intent recover = e.getIntent();
        //startActivityForResult(recover, REQUEST_CODE_TOKEN_AUTH);
    } catch (GoogleAuthException authEx) {
        Log.e(TAG, authEx.toString());
    }

    return token;

错误:

 E/SnepServer(32677): IO error
 E/SnepServer(32677): java.io.IOException
 E/SnepServer(32677):   at com.android.nfc.dhimpl.NativeLlcpServiceSocket.accept(NativeLlcpServiceSocket.java:42)
 E/SnepServer(32677):   at com.android.nfc.snep.SnepServer$ServerThread.run(SnepServer.java:213)
 D/NdefPushServer(32677): stop, thread = Thread[Thread-8152,5,main]
 D/NdefPushServer(32677): shuting down server thread
 E/HandoverServer(32677): IO error
 E/HandoverServer(32677): java.io.IOException
 E/HandoverServer(32677):   at com.android.nfc.dhimpl.NativeLlcpServiceSocket.accept(NativeLlcpServiceSocket.java:42)
 E/HandoverServer(32677):   at com.android.nfc.handover.HandoverServer$ServerThread.run(HandoverServer.java:108)
 E/NdefPushServer(32677): IO error
 E/NdefPushServer(32677): java.io.IOException
 E/NdefPushServer(32677):   at com.android.nfc.dhimpl.NativeLlcpServiceSocket.accept(NativeLlcpServiceSocket.java:42)
 E/NdefPushServer(32677):   at com.android.nfc.ndefpush.NdefPushServer$ServerThread.run(NdefPushServer.java:154)

我用来调用AsyncTask的代码

private void authenticate() {
      String accountEmail="asdf";
      String authToken = "null";
      AccountManager am = AccountManager.get(context);
      Account[] accounts = am.getAccountsByType("com.google");

      if (accounts != null){
          Account account = accounts[0];
          accountEmail = account.name;

          System.out.println(context);
          try {
                authToken=new GetTokens(context).execute(accountEmail).get();
          } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
          } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
          } 
      }
}    

1 个答案:

答案 0 :(得分:0)

尝试一些事情,

  1. 为GooglePlayServicesAvailabilityException设置一个catch
  2. 检查您是否在AndroidManifest.xml中的application元素中放置了正确的元数据 <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
  3. 将RuntimeException的catch作为挖掘崩溃源的最后手段。