5

Ứng dụng của tôi cho phép người dùng đăng nhập bằng Google Plus và nhận tên cũng như địa chỉ email của họ. Tôi đã cố truy cập mã thông báo.Mã thông báo truy cập được truy xuất: null. com.google.android.gms.auth.GoogleAuthException: Unknown

Mã để truy cập mã thông báo:

Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();   
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); 
     AccountManager am = AccountManager.get(this); 

     final Account[] accounts = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE); 

     AsyncTask<Void, Void, String> task2 = new AsyncTask<Void, Void, String>() { 
      public static final int REQUEST_CODE_TOKEN_AUTH = 100; 

      @Override 
      protected String doInBackground(Void... params) { 
       String mScope="audience:server:client_id:899555500747-38rpnq51of946grhdvofck7r8u5p09cd.apps.googleusercontent.com:api_scope:https://www.googleapis.com/auth/plus.login"; 
// Get the token for the current user 
       String token = null; 
       try { 
        token = GoogleAuthUtil.getToken(getApplicationContext(), Plus.AccountApi.getAccountName(mGoogleApiClient), mScope); 
        Log.i("G token", 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) { 
        // The call is not ever expected to succeed 
        // assuming you have already verified that 
        // Google Play services is installed. 
        Log.e(TAG, authEx.toString()); 
       } 
       return token; 
      } 
      @Override 
      protected void onPostExecute(String token) { 
       Log.i(TAG, "Access token retrieved:" + token); 
      } 

     }; 
     task2.execute(); 

Lỗi:

01-27 23:42:14.877 30994-31262/com.unicloud.mittal E/loginWithGooglePlus﹕ com.google.android.gms.auth.GoogleAuthException: Unknown 
01-27 23:42:14.877 30994-30994/com.unicloud.mittal I/loginWithGooglePlus﹕ Access token retrieved:null 

Tôi đã thử các giải pháp khác nhau mà tôi có thể tìm thấy trên stackoverflow. Hiện tại, tôi đang sử dụng ID ứng dụng khách từ "Tài khoản dịch vụ" từ dev console, tôi cũng đã thử sử dụng ID khách hàng cho "ID ứng dụng cho ứng dụng Android", nó vẫn hiển thị lỗi tương tự.

Vui lòng cho tôi biết tôi đang làm gì sai? Cảm ơn.

Trả lời

5

tôi giải quyết vấn đề này bằng cách thay thế dòng

String mScope="audience:server:client_id:899555500747-38rpnq51of946grhdvofck7r8u5p09cd.apps.googleusercontent.com:api_scope:https://www.googleapis.com/auth/plus.login"; 

này với điều này

String mScope = "oauth2:https://www.googleapis.com/auth/plus.login"; 

Tôi đã nhận các thẻ truy cập, và tôi đã tự hỏi nếu tôi đang làm đúng. Vì vậy, tôi đã xác minh nếu mã thông báo là chính xác. Tôi đã cố truy cập địa chỉ này, tôi đã thay thế accessToken bằng mã thông báo tôi đã truy lục.

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=accessToken 

Nó cho tôi thấy loại đầu ra này.

{ 
    "issued_to": "xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com", 
    "audience": "xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com", 
    "user_id": "xxxxxxxxxxxxxxxxxxxxxxx", 
    "scope": "https://www.googleapis.com/auth/userinfo.profile https://gdata.youtube.com", 
    "expires_in": 3019, 
    "access_type": "online" 
    } 

Trong số issued_to nó cho tôi thấy ID ứng dụng khách của tôi cho ứng dụng Android có nghĩa là mã thông báo này được cấp cho id khách hàng của tôi. Vì vậy, tôi cho rằng tôi đang đi đúng hướng.

Các vấn đề liên quan