2013-03-04 34 views
7

私のアプリケーションにGoogle Plusのログインをしたいと思います。私は接続できますが、現在のユーザーのプロファイル情報を取得することができませんでしGoogleプラス:人を読み込むことができません

@Override 
public void onConnected() { 

    String accountName = mPlusClient.getAccountName(); 

    mPlusClient.loadPerson(this, "me"); 
} 

@Override 
public void onPersonLoaded(ConnectionResult status, Person person) { 

    Log.d("onPersonLoaded - ConnectionResult", "" + status); 
    Log.d("onPersonLoaded - person", "" + person); 
    if (status.getErrorCode() == ConnectionResult.SUCCESS) { 
     Log.d(TAG, "Display Name: " + person.getDisplayName()); 
    } 
} 

私はonConnectedにアカウント名を取得する。しかしonPersonLoadedperson

ためnullを与えるログが示すことができる:

onPersonLoaded - ConnectionResult(29861): ConnectionResult{statusCode=NETWORK_ERROR, resolution=null} 
onPersonLoaded - person(29861): null 

からドキュメントhere

public static final int NETWORK_ERROR

ネットワークエラーが発生しました。再試行して問題を解決する必要があります。

定数値:7(0x00000007)

しかし、私は関係なく、私が再試行回数と同じエラーを取得していません。これは私が使用する許可です:

私はこれを解決する方法はありませんし、過去2時間を探しています。どんな助力も高く評価されます、ありがとうございます。

+0

のように、sample codeはPlusClientコンストラクタで範囲を定義で以下のとき、あなたは逃した可能性がもう一つのこと?私は同じ問題に直面している、あなたはこれで私を助けてくださいできますか?回答が見つからなかった場合は、アクティビティコードを共有してください。 –

+0

申し訳ありませんが、私は上記の問題の解決策を見つけることができませんでした。しかし、私は別のアプローチを使って同じことを達成することができました。下の私の答えをチェックしてください。 – user1537779

+0

私は解決策を見つけました。あなたが正しいと信じている場合は、他の人の利益のために見て受け入れてください。 –

答えて

13

最後に私は答えを見つけました。 :)

loadPerson()およびloadPeople()は、有効なキーで署名されたエクスポートされたAPKでのみ期待されるデータを返します。ここで有効なのは、SHAIとあなたのAPI key consoleの他の仕様と一致しなければならないということです。

あなたはそれが働いてしまったの

mPlusClient = new PlusClient.Builder(this, this, this).setScopes(Scopes.PLUS_LOGIN) 
       .setVisibleActivities("http://schemas.google.com/AddActivity").build(); 
+0

jarsignerを使ってapkにサインするのですか? – dd619

+0

私はkeytoolを使用してサインを意味するか、またはeclipseエクスポートのサイン付きapkウィザードを使用しています –

+0

なぜmPlusClient.Builder(....)はnullを返しますか? –

4
private class connectAsyncTask extends AsyncTask<String, Void, String> { 

    String sAccessToken = null; 

    @Override 
    protected void onPostExecute(String info) { 

     Log.d("JSONData = ", "" + info); 
     //This contains all the data you wanted. 
    } 

    @Override 
    protected String doInBackground(String... params) { 

     HttpURLConnection urlConnection = null; 
     try { 
      URL url = new URL(
        "https://www.googleapis.com/oauth2/v1/userinfo"); 
      sAccessToken = GoogleAuthUtil 
        .getToken(
          MainActivity.this, 
          mPlusClient.getAccountName() + "", 
          "oauth2:" 
            + Scopes.PLUS_PROFILE 
            + " https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email"); 

      //This is probably not the right way to do. 
      //What I am doing here is, get an AccessToken, invalidate it and get a new 
      //AccessToken again. Because I couldn't find a way to check whether the 
      //AccessToken is expired or not. 

      GoogleAuthUtil.invalidateToken(MainActivity.this, sAccessToken); 

      sAccessToken = GoogleAuthUtil 
        .getToken(
          MainActivity.this, 
          mPlusClient.getAccountName() + "", 
          "oauth2:" 
            + Scopes.PLUS_PROFILE 
            + " https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email"); 

      urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setRequestProperty("Authorization", "Bearer " 
        + sAccessToken); 

      BufferedReader r = new BufferedReader(new InputStreamReader(
        urlConnection.getInputStream(), "UTF-8")); 
      StringBuilder total = new StringBuilder(); 
      String line; 
      while ((line = r.readLine()) != null) { 
       total.append(line); 
      } 
      line = total.toString(); 
      if (!TextUtils.isEmpty(line)) { 
       return line; 
      } else { 
       return null; 
      } 
     } catch (UserRecoverableAuthException userAuthEx) { 
      // Start the user recoverable action using the intent returned 
      // by getIntent() 

      userAuthEx.printStackTrace(); 
      mActivity.this.startActivityForResult(
        userAuthEx.getIntent(), MY_ACTIVITYS_AUTH_REQUEST_CODE); 
      return null; 
     } catch (FileNotFoundException e) { 
      //You get this exception when the AccessToken is expired. 
      //I don't know how its a FileNotFoundException 
      //Thats why instead of relying on this, I did the above to get 
      //new AccessToken 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return null; 
     } finally { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
     } 
    } 

    @Override 
    protected void onPreExecute() { 
    } 
} 
+0

asynctaskを呼び出すときにメソッドを実行するために渡す文字列は何ですか? –

+1

実際には何もありません。私の 'mPlusClient'はクラス変数なので、AsyncTaskの内部で' AccountName'を取得することができます。しかし、実際には実行中にStringとして 'mPlusClient.getAccountName()'を渡してください。そして、それは後で 'AccessToken'とすべてのデータを取得するために必要なすべてです – user1537779

+0

私をたくさん助けました。おかげです:) –

関連する問題