2016-12-26 10 views
1

내 안드로이드 애플 리케이션에서 페이 스북 로그인을 통해 로그인을 시도하고 있지만 항상 잘못된 키 해시를 제공합니다. 나는 페이스 북의 개발자 사이트에서 주어진 명령을 사용하여 내 맥의 터미널을 통해 제대로 해시 키를 생성하고 또한 properly.Here은 로그인 활동 클래스 내 코드 내 페이스 북의 응용 프로그램 ID를 사용하고 있습니다 :facebook 로그인 조각이 항상 안드로이드에 잘못된 키 해시를 표시하는 이유는 무엇입니까?

LoginActivity.java

package com.titmus.worldscope; 

import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageInfo; 
import android.content.pm.PackageManager; 
import android.content.pm.Signature; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Base64; 
import android.util.Log; 
import android.webkit.WebView; 
import android.widget.Toast; 

import com.facebook.AccessToken; 
import com.titmus.worldscope.model.WorldScopeUser; 
import com.titmus.worldscope.utility.WorldScopeAPIService; 
import com.titmus.worldscope.utility.WorldScopeRestAPI; 

import java.security.MessageDigest; 

import fragment.FacebookLoginFragment; 

import retrofit2.Call; 
import retrofit2.Callback; 
import retrofit2.Response; 

public class LoginActivity extends AppCompatActivity implements FacebookLoginFragment.OnFragmentInteractionListener { 

    private static final String TAG = "LoginActivity"; 
    private static final String WELCOME_GIF_LINK = "file:///android_asset/welcomeGifAssets/welcome.html"; 
    private static final String APP_SERVER_AUTH_FAILED_MSG = "Authentication with WorldScope's server has failed, please check that you have internet connections and try again."; 
    private static Context context; 
    private FacebookLoginFragment facebookLoginFragment; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_facebook_login); 

     context = this; 

     loadGifIntoWebView(); 
    } 

    @Override 
    public void onFacebookLoginSuccess(AccessToken accessToken) { 
     // Successful login -> Redirect to Main activity 
     Log.d(TAG, "Login Success!"); 
     Log.d(TAG, "AccessToken: " + accessToken.getToken()); 

     // Instantiate and make a call to login user into WorldScope servers 
     Call<WorldScopeUser> call = new WorldScopeRestAPI(context).buildWorldScopeAPIService().loginUser(new WorldScopeAPIService.LoginUserRequest(accessToken.getToken())); 
     call.enqueue(new Callback<WorldScopeUser>() { 
      @Override 
      public void onResponse(Response<WorldScopeUser> response) { 
       if(response.isSuccess()) { 
        Log.d(TAG, "Success!"); 
        Log.d(TAG, "" + response.body().toString()); 

        WorldScopeUser user = response.body(); 
        WorldScopeAPIService.setUser(user); 
        // Redirect to MainActivity if successful 
        redirectToMainActivity(); 

       } else { 
        Log.d(TAG, "Failure" + response.code() + ": " + response.message()); 
        // Logout of Facebook 
        logoutOfFacebook(); 
       } 
      } 

      @Override 
      public void onFailure(Throwable t) { 
       Log.d(TAG, "Failure: " + t.getMessage()); 
       // Logout of Facebook 
       logoutOfFacebook(); 
      } 
     }); 
    } 

    //Redirects to MainActivity 
    protected void redirectToMainActivity() { 
     Intent intent = new Intent(context, MainActivity.class); 
     intent.putExtra("activity", TAG); 
     startActivity(intent); 
    } 

    // Called to logout of Facebook when attempt to authenticate with App server fails 
    private void logoutOfFacebook() { 

     if(facebookLoginFragment == null) { 
      // Get FacebookLoginFragment if missing 
      facebookLoginFragment = (FacebookLoginFragment) getSupportFragmentManager().findFragmentById(R.id.facebookLoginButtonFragment); 
     } 

     // Toast to inform user 
     Toast toast = Toast.makeText(context, APP_SERVER_AUTH_FAILED_MSG, Toast.LENGTH_LONG); 
     toast.show(); 
     facebookLoginFragment.logoutFromFacebook(); 

    } 

    // Method to load Gif's html data into WebView 
    private void loadGifIntoWebView() { 
     WebView welcomeGifWebView = (WebView) findViewById(R.id.welcomeGifWebView); 
     welcomeGifWebView.loadUrl(WELCOME_GIF_LINK); 
    } 
} 

activity_facebook_login.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/loginActivity" 
    tools:context="com.titmus.worldscope.LoginActivity"> 

    <WebView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/welcomeGifWebView" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" /> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/facebookLoginButtonFragment" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="48dp"> 

     <ImageView 
      android:layout_width="32dp" 
      android:layout_height="match_parent" 
      android:id="@+id/imageView" 
      android:src="@drawable/ic_logo" 
      android:layout_alignBottom="@+id/textView4" 
      android:layout_toStartOf="@+id/textView4" 
      android:layout_marginEnd="4dp" 
      android:paddingTop="6dp" 
      android:paddingBottom="5dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="WorldScope" 
      android:id="@+id/textView4" 
      android:textColor="#ffffff" 
      android:textStyle="bold" 
      android:textSize="32sp" 
      android:layout_above="@+id/facebookLoginButtonFragment" 
      android:layout_centerHorizontal="true" /> 
    </LinearLayout> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="By logging in you agree to WorldScope&apos;s Privacy Policy and Terms of Use." 
     android:id="@+id/textView6" 
     android:textColor="#eeeeee" 
     android:textSize="14dp" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="16dp" 
     android:textAlignment="center" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="57dp" /> 

    <fragment 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:name="fragment.FacebookLoginFragment" 
     android:id="@+id/facebookLoginButtonFragment" 
     tools:layout="@layout/fragment_facebook_login" 
     android:layout_marginBottom="14dp" 
     android:layout_above="@+id/textView6" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 

답변

1

cmd를 사용하여 처음에 대해 잘 작동 생성 된 keyhash은 그렇지 않습니다.

그래서 그런 genrated있는 키 해시를 복사하면 클래스

public void printHashKey(){ 
     // Add code to print out the key hash 
     try { 
      PackageInfo info = getPackageManager().getPackageInfo(
        "net.simplifiedcoding.androidlogin", 
        PackageManager.GET_SIGNATURES); 
      for (Signature signature : info.signatures) { 
       MessageDigest md = MessageDigest.getInstance("SHA"); 
       md.update(signature.toByteArray()); 
       Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT)); 
      } 
     } catch (PackageManager.NameNotFoundException e) { 

     } catch (NoSuchAlgorithmException e) { 

     } 
    } 

의에서 keyHash를 생성하기 위해이 방법을 사용하고, 페이스 북의 개발자 콘솔에 cmd에 의해 생성 된 keyhash을 추가 곳에 붙여 넣습니다.

페이스 북 개발자 콘솔에서 응용 프로그램을 하나의 장치로 제한하지 마십시오.

+0

facebook 개발자 콘솔에서 옵션이 변경되었습니다. 더 이상 앱을 라이브로 만들 수있는 옵션을 제공하지 않습니다. –

+0

위의 genrate 해시 코드는 콘솔에 추가 할 필요가 없습니다. 그래, 그것을 시도 – Mrinmoy

+0

작동합니다. 잠시만 기다려주십시오 –