Facebook集成无法正常运作

时间:2015-11-28 07:17:43

标签: android facebook-graph-api

我已将facebook sdk集成到我的演示应用程序中以进行Facebook集成。当我点击按钮片段出现并且我能够登录但它没有恢复任何值。我在logcat上没有任何东西。

这是我的代码:

package com.example.manishnegi.facebooklogin;

public class MainActivity extends Activity {
LoginButton btn;
CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_main);
    callbackManager=CallbackManager.Factory.create();

    btn=(LoginButton)findViewById(R.id.login_button);
    btn.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {


        @Override
        public void onSuccess(LoginResult loginResult) {
            Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_SHORT).show();

            AccessToken accessToken=loginResult.getAccessToken();
           GraphRequest request = GraphRequest.newMeRequest(
                    accessToken,
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {

                            try {
                                Toast.makeText(getApplicationContext(),""+object.getString("name"),Toast.LENGTH_SHORT).show();
                                System.out.println( object.getString("name"));
                                System.out.println( object.getString("gender"));

                            }
                            catch (JSONException ex)
                            {
                               System.out.println(ex.getMessage());
                            }

                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "name,gender");
            request.setParameters(parameters);
            request.executeAsync();
        }

        @Override
        public void onCancel() {
            Toast.makeText(getApplicationContext(),"Cancel",Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(FacebookException e) {
            Toast.makeText(getApplicationContext(),"ERROR "+e.getMessage(),Toast.LENGTH_SHORT).show();

        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

请帮助我卡住

1 个答案:

答案 0 :(得分:0)

您还可以像这样生成HASHKEY:

private void getHashKey() {
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                getPackageName(), PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("RAJAT",
                    "KeyHash: "
                            + Base64.encodeToString(md.digest(),
                                    Base64.DEFAULT));
        }
    } catch (NameNotFoundException e) {
    } catch (NoSuchAlgorithmException e) {
    }

}

getHashKey() mthod在onCreate()中调用并在Facebook开发者控制台上使用此哈希键。

并检查在AndroidManifest文件中完成的流动条目

<activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

    <meta-data
        android:name="com.facebook.sdk.ApplicationName"
        android:value="@string/facebook_app_name" />

    <provider
        android:name="com.facebook.FacebookContentProvider"
        android:authorities="com.facebook.app.FacebookContentProviderandyourappidfromfacebookconsole"
        android:exported="true" />

我希望这对你有用......