为什么Google+集成不适用于我的Android应用?

时间:2014-01-06 13:36:33

标签: android google-plus google-play-services

我下载了GooglePlayServices的最新版本并包含在我的应用程序中。我生成了调试密钥的SHA1指纹并在Google API Console中设置它们。那里只有一个应用程序。

我像这样使用Google Plus:

googlePlusClient = new PlusClient.Builder(getActivity(), new GooglePlayServicesClient.ConnectionCallbacks() {
        @Override
        public void onConnected(Bundle bundle) {
            googlePlusClient.loadPeople(new PlusClient.OnPeopleLoadedListener() {

                @Override
                public void onPeopleLoaded(ConnectionResult connectionResult, PersonBuffer persons, String s) {
                    if(connectionResult.isSuccess()) {
                        ArrayList<String> result = new ArrayList<String>();
                        for(Person person : persons) {
                            result.add(person.getId());
                        }
                        // Some actions with the data
                    }
                }
            }, "me");

        }

        @Override
        public void onDisconnected() {

        }
    }, new GooglePlayServicesClient.OnConnectionFailedListener() {
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            if(connectionResult.hasResolution()) {
                try {
                    connectionResult.startResolutionForResult(getActivity(), REQUEST_GOOGLE_AUTH);
                } catch (IntentSender.SendIntentException e) {
                    e.printStackTrace();
                }
            }
        }
    }).setScopes(Scopes.PLUS_PROFILE).build();
    googlePlusClient.connect();

而且:

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(googlePlusClient != null && requestCode == REQUEST_GOOGLE_AUTH && resultCode == Activity.RESULT_OK) {
        googlePlusClient.connect();
    }
}

我仍然收到403错误。这里的LogCat:

E/Volley﹕ [907] ot.a: Unexpected response code 403 for https://www.googleapis.com/plus/v1/people/me
D/GooglePlusPlatform﹕ Unexpected response code (403) when requesting: getPerson
I/GooglePlusPlatform﹕ {"code":403,"errors":[{"message":"Access Not Configured","domain":"usageLimits","reason":"accessNotConfigured"}]}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。我不知道为什么,但我的调试密钥中的指纹是错误的。我用这段代码来获取它们。希望这对其他人有所帮助。

    PackageInfo packageInfo = null;
    try {
        packageInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), PackageManager.GET_SIGNATURES);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    Signature[] signatures = packageInfo.signatures;
    byte[] cert = signatures[0].toByteArray();
    InputStream input = new ByteArrayInputStream(cert);

    CertificateFactory cf = null;
    try {
        cf = CertificateFactory.getInstance("X509");
    } catch (CertificateException e) {
        e.printStackTrace();
    }
    X509Certificate c = null;
    try {
        c = (X509Certificate) cf.generateCertificate(input);
    } catch (CertificateException e) {
        e.printStackTrace();
    }
    try {
        MessageDigest md = MessageDigest.getInstance("SHA1");
        byte[] publicKey = md.digest(c.getPublicKey().getEncoded());

        StringBuffer hexString = new StringBuffer();
        for (int i=0;i<publicKey.length;i++) {
            String appendString = Integer.toHexString(0xFF & publicKey[i]);
            if(appendString.length()==1)hexString.append("0");
            hexString.append(appendString);
        }
        Log.d("SHA1", "Cer: " + hexString.toString());

    } catch (NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    }