2016-01-24 128 views
0

我想要實現與Facebook的登錄我的服務(可通過網絡進行訪問,以及一個原生Android)實現的oauth2登錄與服務器和移動應用

這是我做的Web部件:

  1. 用戶點擊登錄Facebook的
  2. 它被定向到FB的頁面,它會檢查憑據
  3. 它重定向到一個頁面上我的web服務,從細節
  4. 然後,我自己的服務創造供其使用的訪問令牌,並且將其作爲一個JSON輸出到客戶端。

我想知道如何可以使用Android來實現。如果我在我的應用程序中使用Web視圖,用戶將不得不登錄Facebook(我想避免)。相反,如果他使用Facebook應用在Facebook上登錄,它應該直接轉到權限。

如果我使用Android的原生SDK,我不知道如何執行重定向到我自己的服務器,併爲自己的網站的訪問令牌。

回答

0

如果要授予用戶訪問令牌爲您的網站(通過控制面板 - >選擇平臺),那麼你就應該落實Facebook在自己的網站提供的登錄過程,並在你的應用程序設置頁(在你的Facebook開發者帳戶頁面)啓用您的Web應用程序。

快速入門:https://developers.facebook.com/docs/facebook-login/web

+0

這如何回答我的問題? –

+0

因爲爲什麼要使用web視圖獲取用戶登錄到自己的Facebook帳戶,FB SDK爲您提供了原生的Android登錄對話框(您的用戶是否通過FB應用程序登錄或沒有),並在FB應用程序的情況下,不可用,FB做了一個回退,將爲您的用戶提供一個對話框,其中有一個webview可以登錄...所以,根據你上面提到的(你知道的),唯一的邏輯需求是你想讓你的用戶登錄你的網站和你的原生Android應用程序,這就是爲什麼我提供給你的原因在您的答案的這一部分執行登錄網絡 –

0

你可以在原生Android你在網上也做了相同的。

只是Facebook Authentication for android

我也有同樣的情況下,作爲倒下你我已經成功地用我自己的Web服務來實現。

private void doFacebookLogin(View v) { 
     final View snakeBarView = v; 
     callbackManager = CallbackManager.Factory.create(); 
     LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "public_profile")); 
     LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 

       Snackbar.make(snakeBarView, "Facebook Login Success", Snackbar.LENGTH_SHORT).show(); 
       GraphRequest.newMeRequest(
         loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { 
          @Override 
          public void onCompleted(JSONObject json, GraphResponse response) { 
           if (response.getError() != null) { 
            // handle error 
            System.out.println("ERROR"); 
           } else { 
            System.out.println("Success"); 
            try { 
             String jsonresult = String.valueOf(json); 
             String emailId, id, name, profilePicture; 
             if (json.isNull("id")) 
              id = ""; 
             else 
              id = json.getString("id"); 
             if (json.isNull("email")) 
              emailId = id + "@facebook.com"; 
             else 
              emailId = json.getString("email"); 

             if (json.isNull("name")) 
              name = ""; 
             else 
              name = json.getString("name"); 

             profilePicture = getString(R.string.fbProfilePicUrl) + id + "/picture?type=large"; 
             Registration registration = new Registration(); 
             registration.Name = name; 
             registration.EmailId = emailId.toUpperCase(); 
             registration.ProfilePicture = profilePicture; 
             registration.PhoneNumber = ""; 
             registration.Area = ""; 
             registration.Password = ""; 
             ghilliHelper.signupOrLoginPreStep(registration); 
            } catch (JSONException e) { 
             e.printStackTrace(); 
            } 
           } 
          } 

         }).executeAsync(); 
      } 

      @Override 
      public void onCancel() { 
       Snackbar.make(snakeBarView, "Facebook Login Cancelled", Snackbar.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onError(FacebookException e) { 
       Snackbar.make(snakeBarView, "Facebook Login Error", Snackbar.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

成功Facebook登錄後獲取電子郵件ID並從應用程序調用您的服務。

您的服務。

檢查電子郵件ID是存在或不

如果電子郵件ID不存在,創建一個帳戶,並返回一個JSON作爲登錄成功

否則返回JSON作爲登錄成功。

Web服務

[HttpPost] 
     public async Task<string> SocialMediaLogin(Registration reg) 
     { 
      LoginResult logResult = new LoginResult(); 

      try 
      { 
       CloudTableClient tableclient = _storageAccount.CreateCloudTableClient(); 
       CloudTable table = tableclient.GetTableReference(_registrationTable); 

       if (!table.Exists()) 
       { 
        logResult.Failure = true; 
       } 
       else 
       { 
        TableQuery<Registration> query = new TableQuery<Registration>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, reg.EmailId.ToUpper())); 
        Registration reg_obj2 = new Registration(); 
        IEnumerable<Registration> ienum_obj = table.ExecuteQuery(query); 
        if (ienum_obj.Count() != 0) 
        { 
         foreach (var a in ienum_obj) 
         { 
          logResult.Success = true; 
          logResult.UserDetails = a; 
         } 

        } 
        else 
        { 
         RegistrationResult signupResult = await SocialMediaSignup(reg); //Create an Account 
         if (signupResult.Success) 
         { 
          logResult.Success = true; 
          logResult.UserDetails = signupResult.UserDetails; 
         } 
         else if (signupResult.Failure) 
         { 
          logResult.Failure = true; 
         } 
        } 
       } 
      } 
      catch (Exception) 
      { 
       logResult.Failure = true; 
      } 
      var resultString = JsonConvert.SerializeObject(logResult); 
      return resultString; 
     } 
+0

的步驟「檢查電子郵件ID是否存在」,我如何確保其不是惡意的郵寄請求到我的服務? –

+0

@harvey_slash好問題。您可以從證書創建一個AD令牌。在這種情況下,我們可以避免惡意帖子請求。 –

+0

的任何例子? –

相關問題