huuanh1987 / facebook-java-api

Automatically exported from code.google.com/p/facebook-java-api
0 stars 0 forks source link

I am not getting logged user infomrmation using facebook-java-api-3.0.0-SNAPSHOT #224

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. users_getInfo
2. i am getting  userid and session secret key
3. using xml formate to getting user information

What is the expected output? 
Logged in user information with full name and email id 

What do you see instead?
Getting user id and session secret key after that getting error
"com.google.code.facebookapi.FacebookException: Incorrect signature" 

What version of the product are you using? On what operating system?
Operationg System: Windows Xp
 version of the product: 3.0.0-SNAPSHOT

After that i will deploy my application on centOS

Please provide any additional information below.

Original issue reported on code.google.com by dinesh....@gmail.com on 14 Jul 2009 at 11:43

GoogleCodeExporter commented 8 years ago
package face;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;

import com.google.code.facebookapi.FacebookException;
import com.google.code.facebookapi.FacebookSignatureUtil;
import com.google.code.facebookapi.FacebookXmlRestClient;
import com.google.code.facebookapi.ProfileField;
import com.google.code.facebookapi.schema.FriendsGetResponse;
import com.google.code.facebookapi.schema.User;
import com.google.code.facebookapi.schema.UsersGetInfoResponse;

public class FaceBookXMLTest extends HttpServlet {
    @Override
    public void init() throws ServletException {
        // TODO Auto-generated method stub
        super.init();
    }
      static String API_KEY = "d707ab3c7a11bebcab97488d943c0424";
        static String SECREATE_KEY = "b1c82e8b932ed5b9c9290a9c7312f067";
        static String SESSION_KEY;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub

        try {

            FacebookXmlRestClient client = new FacebookXmlRestClient( API_KEY , 
SECREATE_KEY);
            client.setIsDesktop(true);

            // Get the auth_token

            String token = client.auth_createToken();
            System.out.println("Token: " + token);

            // Generate signature

            Map<String,String> entries = new HashMap<String,String>();
            entries.put("api_key", "d707ab3c7a11bebcab97488d943c0424");
            entries.put("v", "1.0");
            entries.put("auth_token", token);

            // Convert the map to a list

            List<String> params = FacebookSignatureUtil.convert( entries.entrySet() );

            // Sort the list

            Collections.sort( params );

            // Concatenate params for URL encoding (to generate a URL)

            String paramURLEncoded = "";

            for ( String param : params ) {

                // Don't add an ampersand if it's the first param

                if ( paramURLEncoded == "" ) {

                    paramURLEncoded = paramURLEncoded + param;

                } 

                // Add an ampersand

                else {

                    paramURLEncoded = paramURLEncoded + "&" + param;

                }

            }

            // Generate the signature with the sorted list of params

            String generatedSignature = FacebookSignatureUtil.generateSignature(
params, "b1c82e8b932ed5b9c9290a9c7312f067" );
            System.out.println("Generated Signature: " + generatedSignature);

            // Generate the URL and browse to it so the user can log in

            paramURLEncoded = paramURLEncoded + "&sig=" + generatedSignature;
            String url = "http://www.facebook.com/login.php?" + paramURLEncoded;
            System.out.println("URL: " + url);
            BrowserLauncher.openURL(url);

            // Wait for user to log in and hit enter

            System.out.print("Log in to Facebook. When you're done, press ENTER.");
           // System.in.read();

            // Get the session secret key

            String sessionSecret = client.auth_getSession(token);        
            System.out.println("Session secret key is " + sessionSecret);
            SESSION_KEY = sessionSecret; 
            // Get the logged in user's ID

            Long userId = client.users_getLoggedInUser();
            System.out.println("Getting friends for user " + userId);
            printUserInfo(userId,client,SESSION_KEY);
            // Get friends list

            client.friends_get();

            FriendsGetResponse response = (FriendsGetResponse) client.getResponsePOJO();
            List<Long> friends = response.getUid();

            // Go fetch the information for the user list of user ids

            client.users_getInfo(friends, EnumSet.of(ProfileField.NAME));

            UsersGetInfoResponse userResponse = (UsersGetInfoResponse)
client.getResponsePOJO();

            // Print out the user information
System.out.println(userResponse+" =userResponse  ");
            List<User> users = userResponse.getUser();
            for (User user : users) {
                System.out.println(user.getName());
            }

        } catch (FacebookException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

     private static String printUserInfo(Long uid, FacebookXmlRestClient client, String
sessionKey) throws FacebookException
        {
            StringBuffer ret = new StringBuffer();
            //init array parameter
            List<Long> uids = new ArrayList<Long>(1);
            uids.add(uid);
            //init field parameter - we choose all profile infos.
            List<ProfileField> fields = Arrays.asList(ProfileField.values());

            //init the client in order to make the xml request
            client = new FacebookXmlRestClient(API_KEY , SECREATE_KEY, SESSION_KEY);
            //get the xml document containing the infos
            Document userInfoDoc = client.users_getInfo(uids, fields);

            //for each info append it to returned string buffer
            for (ProfileField pfield : fields)
            {
                ret.append(pfield.fieldName()).append("
").append(userInfoDoc.getElementsByTagName(pfield.fieldName()).item(0).getTextCo
ntent()).append("");
                ret.append("</br>");
            }
            return ret.toString();
        }

}

This is code what i am deploying

Original comment by dinesh....@gmail.com on 14 Jul 2009 at 12:00