簡體   English   中英

如何使用Facebook iOS SDK 4.x“重新請求”電子郵件權限?

[英]How to “rerequest” email permission using Facebook iOS SDK 4.x?

我正在整合FacebookSDK 4.x與自定義UI的集成,並使用以下方法登錄並獲得用戶的電子郵件權限。

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error){
            NSLog(@"%@",[error localizedDescription]);            
        }
        else if (result.isCancelled){
            NSLog(@"Cancled");
        }
        else
        {
            if ([result.grantedPermissions containsObject:@"email"])
            { 
                NSLog(@"Granted all permission");
                if ([FBSDKAccessToken currentAccessToken])
                {
                    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
                    {
                        if (!error)
                        {
                            NSLog(@"%@",result);
                        }
                    }];
                }
            }
            else
            {
                NSLog(@"Not granted");
            }
        }
    }];

除非用戶拒絕訪問“電子郵件”,否則這很有效。 我在FacebookSDK文檔中看到,我可以重新請求訪問用戶的電子郵件一次。 根據FB文檔:在此鏈接中給出

If someone has declined a permission for your app, the login dialog won't let your app re-request the permission unless you pass auth_type=rerequest along with your request.

啟用重新身份驗證

在您選擇的登錄流程中,我們向您展示了如何使用登錄對話框和OAuth對某人進行身份驗證並從中請求權限。 要重新進行身份驗證,您可以使用這些相同的步驟和其他參數來強制執行:

auth_type:此參數指定請求的身份驗證功能(以逗號分隔的列表)。 有效選項包括:https - 檢查是否存在安全的Facebook會話,並要求重新驗證(如果不存在重新驗證) - 要求此人無條件地重新進行身份驗證

 auth_nonce: includes an app generated alphanumeric nonce which can be used to provide replay protection. See how to check an auth_nonce 

更多。

以下是使用JavaScript SDK的示例,該SDK使用auth_type of reauthenticate觸發重新身份驗證:

FB.login(函數(響應){//原始FB.login代碼},{auth_type:'reauthenticate'})

請注意,響應的主體包含您指定的auth_type參數,例如:

=的access_token&USER_ACCESS_TOKEN期滿= SECONDS_UNTIL_TOKEN_EXPIRES&AUTH_TYPE =重新驗證

如何通過iOS SDK將“auth_type = rerequest”傳遞給Facebook的服務器? 那有什么特別的方法嗎?

我通過回憶方法解決了問題:

[login logInWithReadPermissions:@ [@“email”] handler:^(FBSDKLoginManagerLoginResult * result,NSError * error)

我需要再次請求時傳遞auth_type:'rerequest' ,我在這個方法的文檔中得到它我得到描述:

在要求讀取權限時使用此方法。 您應該只在需要時請求權限並向用戶解釋該值。 您可以檢查result.declinedPermissions,以便在用戶拒絕權限時向用戶提供更多信息。

如果[FBSDKAccessToken currentAccessToken]不為nil,則將其視為該用戶的重新授權,並將“rerequest”標志傳遞給登錄對話框。

只是問題是我正在調用FbSDKLoginManager類的logout方法。 這將清除令牌,它將把它作為舊權限而不是重新請求的權限。

您實際上不需要傳遞或調用auth_type:rerequest。 這已經由facebook完成了。 如果您檢查FBSDKLoginManager,代碼會為您執行此操作。

-(NSDictionary *)logInParametersWithPermissions:(NSSet *)permissions
{
    if ([FBSDKAccessToken currentAccessToken]) 
       {
             loginParams[@"auth_type"] = @"rerequest";
       }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM