基本的Facebook API登录无效

时间:2015-05-19 22:22:42

标签: android facebook facebook-android-sdk

我是Android开发和Facebook SDK的新手。我只是想将Facebook loginButton与我的应用程序联系起来。当我点击按钮时,它会询问我的登录详细信息和权限,然后它会成功返回到应用程序。但在我点击该按钮的同一时刻再次请求我再次登录。

    package com.example.pranavjain.pranavapp;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

import org.json.JSONObject;


public class MainActivity extends ActionBarActivity {

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





    }





   public void onCreateView(
            LayoutInflater inflater,
            ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_main, container, false);

        LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
        loginButton.setReadPermissions("user_friends");
        // If using in a fragment

        // Other app specific specialization

        // Callback registration

       CallbackManager callbackManager = CallbackManager.Factory.create();

       loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
           @Override
           public void onSuccess(LoginResult loginResult) {
               // App code


               GraphRequest request = GraphRequest.newMeRequest(
                       loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                           @Override
                           public void onCompleted(
                                   JSONObject object1,
                                   GraphResponse response) {
                               // Application code
                               Log.d("tagpranav", object1.toString());
                           }
                       });
               Bundle parameters = new Bundle();
               parameters.putString("fields", "id,name,link");
               request.setParameters(parameters);
               request.executeAsync();

           }

           @Override

           public void onCancel() {
               // App code
               Log.v("LoginActivity", "cancel");
           }

           @Override
           public void onError(FacebookException exception) {
               // App code
               Log.v("LoginActivity", exception.getCause().toString());
           }
       });





   }




    @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);
    }
}

我已经检查过Hash键是对的。没有任何东西记录在&#34; tagpranav&#34;或&#34; LoginActivity&#34;。

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:2)

请务必在POST <web service url> HTTP/1.1 Content-Type: text/xml; charset=utf-8 SOAPAction: <action url> Host: <host> Content-Length: xxx 方法中添加以下第一行:

onCreate()

考虑使用FacebookSdk.sdkInitialize(getApplicationContext()); 的{​​{1}}方法移动代码。下面我提到了工作样本。

onCreate()

在上面的代码中,MainActivity是具有所有权限的数组。

您缺少的一件事是,制作public class MainActivity extends ActionBarActivity { CallbackManager callbackManager; @Override protected void onCreate(Bundle savedInstanceState) { LoginButton loginButton = (LoginButton) findViewById(R.id.login_button); loginButton.setReadPermissions(PERMISSIONS_ARRAY); callbackManager = CallbackManager.Factory.create(); // Callback registration loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { Log.i(TAG, "Logged in..."); GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted(JSONObject user, GraphResponse response) { try { if (user != null) { Log.e(TAG, "UserID : " + user.getString("id")); } } catch (Exception e) { e.printStackTrace(); } } }).executeAsync(); } @Override public void onCancel() { // Login canceled } @Override public void onError(FacebookException exception) { // Login error } }); } } 类变量并在PERMISSIONS_ARRAY中添加以下方法。

callbackManager

修改-1:

类变量是在方法的类和方法中定义的东西,因此类的所有方法都可以访问它。 在您的代码中,您已在方法中定义了MainActivity,因此您无法在@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); Log.i(TAG, "OnActivityResult...:" + resultCode); } 中访问它。我在答案中做了相应的修改。