简体   繁体   中英

Error when trying to show Facebook Android SDK Add Friend Dialog

I'm trying to show a add friend dialog via the fb sdk.

                Bundle parameters = new Bundle();
                parameters.putString("id", i.getUid());
                FacebookSession.getSession().dialog(getSherlockActivity(), "friends", parameters,
                          new Facebook.DialogListener()
                          {
                            public void onFacebookError( FacebookError e ) {   }
                            public void onError(DialogError e) {   }
                            public void onCancel() {  }
                            @Override
                            public void onComplete(Bundle values) {
                                // TODO Auto-generated method stub

                            }
                          } );

There is a loading screen and a dialog frame appears, but the content is just:

The redirect_uri URL protocol must be HTTP or HTTPS

Usually a redirect_uri has not to be specified when creating a facebook dialog. Even when I try to specify one manually, for example with:

parameters.putString("redirect_uri", "http://www.facebook.com");

it returns the same error.

Anyone any ideas?

Facebook supports only feed, Oauth and Apprequest actions in a "dialog" request. For these requests even if the redirect_uri is specified as "fbconnect://success", it will not give the error "redirect_uri URL protocol should be http or https". Unfortunately when we issue a friend dialog facebook is excepting the redirect_uri to be http/https and also redirect_uri should have the domain name defined in the "SITE URL" parameter in APP settings.

Anyways, we fixed this issue by overriding the redirect_uri in facebook SDK. In facebook.java we made the following changes to the dialog method:

public void dialog(Context context, String action, Bundle parameters,
    final DialogListener listener) {

String endpoint = DIALOG_BASE_URL + action;
parameters.putString("display", "touch");

Start code change--->> Here you are overriding the redirect_uri with your SITE URL if the action is friends

if(action.contentEquals("friends")) 
{
  parameters.putString("redirect_uri", "http://www.yourdomain.com");
} 
else 
{
  parameters.putString("redirect_uri", REDIRECT_URI);
}

End code change

if (action.equals(LOGIN)) {
    parameters.putString("type", "user_agent");
    parameters.putString("client_id", mAppId);
} else {
    parameters.putString("app_id", mAppId);
}

if (isSessionValid()) {

Start code change--->> We don't need access_token during a friend dialog

   if(!action.contentEquals("friends")) 
         parameters.putString(TOKEN, getAccessToken());

End code change

}
String url = endpoint + "?" + Util.encodeUrl(parameters);
if (context.checkCallingOrSelfPermission(Manifest.permission.INTERNET)
        != PackageManager.PERMISSION_GRANTED) {
    Util.showAlert(context, "Error",
            "Application requires permission to access the Internet");
} else {
    new FbDialog(context, url, listener).show();
}

} Another tricky part is in FbDialog.java there is a Webview client that process the return code from facebook you have to make the following change to FbDialog.java:

private class FbWebViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    Util.logd("Facebook-WebView", "Redirect URL: " + url);
    if (url.startsWith(Facebook.REDIRECT_URI)) {
        Bundle values = Util.parseUrl(url);

        String error = values.getString("error");
        if (error == null) {
            error = values.getString("error_type");
            Util.logd("Facebook-WebViewError", "error type: " + error);
        }
        else
              Util.logd("Facebook-WebViewError", "error: " + error);
        if (error == null) {
            mListener.onComplete(values);
        } else if (error.equals("access_denied") ||
                   error.equals("OAuthAccessDeniedException")) {
            mListener.onCancel();
        } else {
            mListener.onFacebookError(new FacebookError(error));
        }

        FbDialog.this.dismiss();
        return true;
    } else if (url.startsWith(Facebook.CANCEL_URI)) {
        mListener.onCancel();
        FbDialog.this.dismiss();
        return true;
    } else if (url.contains(DISPLAY_STRING)) {
        return false;
    }

Start code change--->> During a friends dialog, redirect_uri is your SITE URL, so check for that in the url variable and process success and failure accordingly

    else  if (url.startsWith("http://www.yourdomain.com")) {
       Bundle values = Util.parseUrl(url);

         String error = values.getString("error");
         if (error == null) {
             error = values.getString("error_type");
             Util.logd("Facebook-WebViewError", "error type: " + error);
         }
         else
            Util.logd("Facebook-WebViewError", "error: " + error);
         if (error == null) {
             mListener.onComplete(values);
         } else if (error.equals("access_denied") ||
                    error.equals("OAuthAccessDeniedException")) {
             mListener.onCancel();
         } else {
             mListener.onFacebookError(new FacebookError(error));
         }

         FbDialog.this.dismiss();
         return true;
    }
   // launch non-dialog URLs in a full browser
   // getContext().startActivity(
   //         new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

End code change

    return false;
}

I filed a bug report at developers.facebook.com and got the following answer:

Our SDKs do not support adding friends. Refer here >(https://developers.facebook.com/docs/reference/androidsdk/) for our documentation on what is >supported.

If you still run across problems, feel free to post at our community website >(http://facebook.stackoverflow.com/) and tag with "android".

Thanks, Jesse

which I don't understand because there is a dialog for adding friends and now it's not supported

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM