1

在一个 Android 应用程序中,我遵循了访问 Google API的示例,到目前为止GoogleApiClient连接成功并调用了onConnected回调。

然而,谷歌的例子停在那里,我不知道如何检索自己的姓名性别位置(在我前段时间使用的 Google+ 的 JavaScript+PHP SDK 中也称为我)。

这是Java代码(为简洁起见跳过了重试和旋转处理,它似乎有效)为什么me为空?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_PROFILE)
            .build();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) { // why is me null?

    Person me = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);

    Person.Name name = me.getName();
    String given = name.getGivenName();
    String family = name.getFamilyName();

    Log.d(TAG, "Given: " + given + ", Family: " + family);
}

请告知如何从 Google+ 获取登录用户的数据

更新:

我试图解决我的问题:

1)我已添加.addScope(Plus.SCOPE_PLUS_LOGIN)到构建器调用中。

2) 我向 AndroidManifest.xml 添加了 2 个权限:

<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

Google Developers Console / Credentials中,我将密钥从我的真实密钥库和 .android\debug.keystore 中放入:

安慰

我不明白在哪里/是否使用上面显示的“客户 ID”?

而且我只启用了 2 个 API:Google+ 和 GCM - 可能更需要?

api

3)最后,我打电话给

adb shell setprop log.tag.GooglePlusPlatform VERBOSE

并看到以下错误:

E/Volley  ( 3890): [594] BasicNetwork.performRequest: Unexpected response code 403 for https://www.googleapis.com/plus/v1/people/me
D/GooglePlusPlatform( 3890): Unexpected response code (403) when requesting: getPerson
W/GooglePlusPlatform( 3890): {"errors":[{"domain":"usageLimits","reason":"accessNotConfigured","message":"Access Not Configured. The API (Google+ API) is not enabled for your project. Please use the Google Developers Console to update your configuration."}],"code":403}

我使用的是真实设备 (HTC M9),我的应用程序尚未发布。

4

2 回答 2

0

我通过查看解决了我的问题

adb shell setprop log.tag.GooglePlusPlatform VERBOSE
adb logcat

输出并在Google Developers Console / Credentials中启用Google Play Android Developer API(之前缺少该 API):

安慰

这是代码:

@Override
public void onConnected(Bundle bundle) {
    Person me = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
    if (me != null) {
        Person.Name name = me.getName();
        String given = name.getGivenName();
        String family = name.getFamilyName();
        boolean female = (me.hasGender() && me.getGender() == 1);

        String photo = null;
        if (me.hasImage() && me.getImage().hasUrl()) {
            photo = me.getImage().getUrl();
        }

        String city = "Unknown city";
        List<Person.PlacesLived> places = me.getPlacesLived();
        if (places != null) {
            for (Person.PlacesLived place : places) {
                city = place.getValue();
                if (place.isPrimary())
                    break;
            }
        }

        Log.d(TAG, "Given: " + given + 
                 ", Family: " + family + 
                 ", Female: " + female + 
                 ", City: " + city);
    }
}
于 2015-08-20T18:14:36.640 回答
0

你可以试试这个方法吗?如果有帮助

public void onConnected(Bundle bundle) {

  Person me = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);

  if (me != null) {

    String personName = me.getDisplayName();         

    try {
        int genderInt = me.getGender();//0 for male, and 1 for female 
        String   gender     = String.valueOf(genderInt);
        String birthday = me.getBirthday();

        System.out.println("gender" + gender + "bod" + birthday);
    } catch (Exception e) {
        e.printStackTrace();
    }       

    String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
  }
}
于 2015-08-20T14:48:40.647 回答