首頁>Program>source

在我的Android應用程式中,我開發了此代碼以使用我的帐戶登錄並获取使用者屬性,例如名稱,位置和電子郵件.問题是我可以获取名稱,但無法获取電子郵件和位置.当我尝試我的代碼而没有尝試捕获應用程式迷恋和我在 getproperty("email")中的日志點時 和 getlocation() .当我使用尝試.该應用程式可以執行,但是没有電子郵件或位置。

public class Share extends Fragment {private static final String TAG ="Share";private UiLifecycleHelper uiHelper;
private View otherView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // To maintain FB Login session
        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.share, container, false);
        // Looks for Login button
        LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
        authButton.setFragment(this);
        // Set View that should be visible after log-in invisible initially
        otherView = view.findViewById(R.id.other_views);
        otherView.setVisibility(View.GONE);
        //authButton.setReadPermissions(Arrays.asList("user_likes", "user_status","email","user_birthday"));
        return view;
    }
    // Called when session changes
    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state,Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };
    // When session is changed, this method is called from callback method
    private void onSessionStateChange(Session session, SessionState state,Exception exception) {
        final TextView name = (TextView) getView().findViewById(R.id.name);
        final TextView mail = (TextView) getView().findViewById(R.id.mail);
        final TextView location = (TextView) getView().findViewById(R.id.location);
        final TextView locale   = (TextView) getView().findViewById(R.id.locale);
        final TextView info = (TextView)getView().findViewById(R.id.msginfo);
        final LinearLayout views= (LinearLayout)getView().findViewById(R.id.other_views);
        if (state.isOpened()) {
            Log.i(TAG, "Logged in...");
            // make request to the /me API to get Graph user
            views.setVisibility(View.VISIBLE);
            info.setText("You can now share images in facebook ");
            Request.newMeRequest(session, new Request.GraphUserCallback() {
                // callback after Graph API response with user
                // object
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if (user != null) {
                        try {
                            // Set view visibility to true
                            otherView.setVisibility(View.VISIBLE);
                            // Set User name
                            name.setText("Hello " + user.getName());
                            // Set Email
                            mail.setText("Your Email: " + user.getProperty("email").toString());
                            locale.setText("Locale: " + user.getProperty("locale").toString());
                            location.setText("Your Current Location: " + user.getLocation().getProperty("name").toString());
                        }
                        catch(Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).executeAsync();
        } else if (state.isClosed()) {
            views.setVisibility(View.INVISIBLE);
            info.setText("If you want to share images in Facebook, please Login");
            Log.i(TAG, "Logged out...");
            otherView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
        Log.i(TAG, "OnActivityResult...");
    }
    @Override
    public void onResume() {
        super.onResume();
        uiHelper.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }
}
最新回復
  • 5月前
    1 #

    問题是您没有要求權限:

    authButton.setReadPermissions(Arrays.asList("user_likes", "user_status","email","user_birthday"));
    

    但是,您使用的是较旧的Facebook SDK,而最新的SDK是4.0。+.下面,我將基於最新的API為您提供Facebook登錄的完整示例代碼.請記住,您首先必须在 developers.facebook中添加您的應用程式 如文件所述。

    public class LoginActivity extends ActionBarActivity{
    @Override
    protected void onActivityResult(int requestCode, int responseCode, Intent data)
    {
        super.onActivityResult(requestCode, responseCode, data);
        callbackManager.onActivityResult(requestCode, responseCode, data);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(this.getApplicationContext());
        setContentView(R.layout.activity_login);
        callbackManager = CallbackManager.Factory.create();
        loginButton = (LoginButton) findViewById(R.id.loginFaceBook_button);
        List<String> permissionNeeds = Arrays.asList("user_photos", "email", "user_birthday", "public_profile");
        loginButton.setReadPermissions(permissionNeeds);
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
        {
            @Override
            public void onSuccess(LoginResult loginResult)
            {
                System.out.println("onSuccess");
                GraphRequest request = GraphRequest.newMeRequest
                        (loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
                        {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response)
                            {
                                // Application code
                                Log.v("LoginActivity", response.toString());
                                //System.out.println("Check: " + response.toString());
                                try
                                {
                                    String id = object.getString("id");
                                    String name = object.getString("name");
                                    String email = object.getString("email");
                                    String gender = object.getString("gender");
                                    String birthday = object.getString("birthday");
                                    System.out.println(id + ", " + name + ", " + email + ", " + gender + ", " + birthday);
                                }
                                catch (JSONException e)
                                {
                                    e.printStackTrace();
                                }
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender, birthday");
                request.setParameters(parameters);
                request.executeAsync();
            }
            @Override
            public void onCancel()
            {
                System.out.println("onCancel");
            }
            @Override
            public void onError(FacebookException exception)
            {
                System.out.println("onError");
                Log.v("LoginActivity", exception.getCause().toString());
            }
        });
      }
    }
    

    如果您想使用 Fragment 代替 ActionBarActivity ,只需添加 loginButton.setFragment(this); 就在您的權限行之後。

    manifest.xml:

    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <application
        <!-- your other attrs..-->
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id"/> <!-- Get this one from developers.facebook -->
        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:label="@string/app_name"/>
    

    您需要將 hash key添加到您的應用程式中 太.這是使用代碼執行此操作的方法:

    try
    {
        //paste Your package name at the first parameter
        PackageInfo info = getPackageManager().getPackageInfo("PUT_YOUR_PACKAGE_NAME_HERE",
                PackageManager.GET_SIGNATURES);
        for (android.content.pm.Signature signature : info.signatures)
        {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            String sign = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            Log.e("MY KEY HASH:", sign);
            Toast.makeText(getApplicationContext(),sign,     Toast.LENGTH_LONG).show();
        }
    }
    catch (PackageManager.NameNotFoundException e)
    {
    }
    catch (NoSuchAlgorithmException e)
    {
    }
    

    在print出雜湊键後,將其複製粘贴到 facebook.developer 帐戶,您的專案所在的位置。

    在Grandle中,您應该添加 jcenterrepositories 並添加 compile 'com.facebook.android:facebook-android-sdk:4.0.0'dependecies

    buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
    }
    allprojects 
    {
    repositories {
        jcenter()
        /*more project attrs..*/
      }
    }
    

    另一个大檔案:

    apply plugin: 'com.android.application'
    android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    defaultConfig {
        applicationId "YOUR_PACKAGE_NAME"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    }dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.facebook.android:facebook-android-sdk:4.0.0'
    }
    

    編輯:

    為了跟蹤使用者的位置,您將需要一个GPS跟蹤器,類似這樣. "user_location" 權限不返迴 lon, lat ,但是Page物件是我想要的物件.因此,您的權限應為 List<String> permissionNeeds = Arrays.asList("user_photos", "email", "user_birthday", "public_profile"); 現在您應该能够檢索使用者的電子郵件

  • c#:使用MediaElement播放Stream中的视频
  • checkbox:ANDROID-ExpandableListView