CODE JSP FACEBOOK LOGIN
WAPMILD (Admin) Pesan : Jika Kamu ingin copas sertakan link artikel ini iya CODE JSP FACEBOOK LOGIN |
In this video I have shown how you can generate Access token dynamically. And use the dynamic access to get facebook user info using facebook graph API.
Here is the steps what you have to
follow.
1. Login to facebook developers site and get an
APP id by creating a facebook APP.
2. Grant permission for your Domain to
access the Facebook app. You can go to this link
Grant Permission for the
steps.
3. Project Structure in Eclipse IDE
4.Update the APP ID in the page
index.jsp
index.jsp
<
span> html >
<
span> head >
<
span> title > Facebook
Login JavaScript Example title
span> >
span>
<
span> meta charset =
span> "
span> UTF-8 " >
span> head >
<
span> body >
<
span> script >
// This is called
with the results from from FB.getLoginStatus()
.
function
statusChangeCallback (
span> response ) {
console .
span> log
span> (
span> 'statusChangeCallback' ) ;
console .
span> log
span> (
span> response ) ;
console .
span> log
span> (
span> response . authResponse .
span> accessToken ) ;
//alert
(response.authResponse.accessToken);
if (
span> response . status === 'connected' ) {
window .
span> location . href = 'Sign_in_Controller.jsp?access_token='
span> +
span> response . authResponse .
span> accessToken ;
}
else
{
// The person is
not logged into your app or we are unable to
tell.
document .
span> getElementById ( 'status' ) . innerHTML
= 'Please log ' +
'into this app.'
span> ;
span>
}
}
// This function
is called when someone finishes with the
Login
// Button. See
the onlogin handler attached to it in the
sample
// code
below.
function
checkLoginState ( ) {
FB .
span> getLoginStatus ( function (
span> response ) {
statusChangeCallback (
span> response ) ;
}
span> )
span> ;
span>
}
window .
span> fbAsyncInit = function ( ) {
FB .
span> init
span> (
span> {
span>
appId :
span> 'FILL THE APP
ID' ,
span>
cookie :
span> true
span> ,
span> // enable
cookies to allow the server to access
// the session
span>
xfbml :
span> true
span> ,
span> // parse
social plugins on this page
version :
span> 'v2.8'
span> // use
graph api version 2.8
}
span> )
span> ;
span>
FB .
span> getLoginStatus ( function (
span> response ) {
statusChangeCallback (
span> response ) ;
}
span> )
span> ;
span>
}
span> ;
span>
// Load the SDK
asynchronously
(
span> function
span> (
span> d ,
span> s ,
span> id )
span> {
span>
var
js , fjs
=
d .
span> getElementsByTagName
span> (
span> s )
span> [
span> 0
span> ]
span> ;
span>
if ( d . getElementById
span> (
span> id )
span> )
span> return
span> ;
span>
js =
d .
span> createElement ( s ) ; js . id = id ;
js .
span> src =
span> "https://
connect.facebook.net/en_US/sdk.js"
span> ;
span>
fjs .
span> parentNode . insertBefore ( js , fjs ) ;
}
span> (
span> document , 'script' , 'facebook-jssdk' ) ) ;
// Here we run
a very simple test of the Graph API after login
is
// successful.
See statusChangeCallback() for when this call is
made.
script >
<
span> fb:
span> login-button scope = " public_
profile,email " onlogin = " checkLoginState(); "
span> >
span>
span> fb:
span> login-button >
<
span> div id =
span> "
span> status " >
span> div >
span> body >
span> html >
5. Sign_in_Controller.jsp
<%@page
import="com.chillyfacts.com.profile.Profile_
Bean"%>
<%@page
import="com.chillyfacts.com.profile.Profile_
Modal"%>
<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<
span> html >
<
span> head >
<
span> meta http-equiv = " Content-Type "
content
span> = " text/html;
charset=ISO-8859-1 " >
<
span> title > Insert title
here title >
span> head >
<
span> body >
<%
String access_token=(String)
request.getParameter("access_token");
Profile_Modal obj_Profile_Modal=new Profile_
Modal();
Profile_Bean obj_Profile_Bean= obj_Profile_
Modal.call_me(access_token);
%>
Name : <%=obj_Profile_Bean.getUser_name
() %> < br >
Email : <%=obj_Profile_Bean.getEmail() %
> < br >
id : <%=obj_Profile_Bean.getId() %> <
span> br >
Profile Picture : <%=obj_Profile_
Bean.getProfile_picture() %> <
span> br >
<
span> img src =
span> "
span> <%=obj_Profile_Bean.getProfile_
picture() %> " >
span> img >
span> body >
span> html >
6. Profile_Bean.java
package
com .
span> chillyfacts . com . profile ;
public
class
Profile_Bean
span> {
span>
private String user_
name ;
span>
private String email ;
private String profile_
picture ;
span>
private String id ;
public String getUser_name
span> (
span> )
span> {
span>
return user_name ;
}
span>
public void setUser_name ( String user_
name )
span> {
span>
this . user_name = user_
name ;
span>
}
span>
public String getEmail ( ) {
return email ;
}
span>
public void setEmail ( String email ) {
this . email = email ;
}
span>
public String getProfile_picture
span> (
span> )
span> {
span>
return profile_picture ;
}
span>
public void setProfile_picture ( String
profile_picture ) {
this . profile_picture = profile_
picture ;
span>
}
span>
public String getId ( ) {
return id ;
}
span>
public void setId ( String id ) {
this . id = id ;
}
span>
}
7.Profile_Modal.java
package
com .
span> chillyfacts . com . profile ;
import
java .
span> io .
span> BufferedReader ;
import
java .
span> io .
span> InputStreamReader ;
import
java .
span> net .
span> HttpURLConnection ;
import
java .
span> net .
span> URL ;
span>
import
org .
span> json .
span> JSONObject ;
public
class
Profile_
Modal {
public Profile_Bean call_me ( String
access_token )
throws Exception {
String url = "https://graph.facebook.com/v2.12/
me?fields=id,name,picture,email&access_
token="
+ access_token ;
URL obj = new URL ( url ) ;
HttpURLConnection con = (
span> HttpURLConnection ) obj . openConnection ( ) ;
//
optional default is GET
con . setRequestMethod ( "GET" ) ;
//
add request header
con . setRequestProperty ( "User-Agent"
span> ,
span> "Mozilla/5.0" ) ;
int
span> responseCode = con . getResponseCode ( ) ;
System . out . println ( "\nSending 'GET' request to URL : "
span> +
url )
span> ;
span>
System . out . println ( "Response Code : " +
responseCode ) ;
BufferedReader in = new BufferedReader
span> (
span>
new InputStreamReader ( con . getInputStream
span> (
span> )
span> )
span> )
span> ;
span>
String inputLine ;
StringBuffer response = new StringBuffer
span> (
span> )
span> ;
span>
while ( ( inputLine = in . readLine br
Email : < < br >
id : < <
span> br >
Profile Picture : < <
span> br >
<
span> img src =
span> "
span> < " >
span> < img >
<
span> body >
<
span> html >
6. Profile_Bean.java
package
com .
span> chillyfacts . com . profile ;
public
class
Profile_Bean
span> {
span>
private String user_
name ;
span>
private String email ;
private String profile_
picture ;
span>
private String id ;
public String getUser_name
span> (
span> )
span> {
span>
return user_name ;
}
span>
public void setUser_name ( String user_
name )
span> {
span>
this . user_name = user_
name ;
span>
}
span>
public String getEmail ( ) {
return email ;
}
span>
public void setEmail ( String email {
this . email = email ;
unction"> println ( "URL : " + data_response . getString ( "url" ) ) ;
obj_Profile_Bean . setProfile_picture
span> (
span> data_response . getString ( "url" ) ) ;
return obj_Profile_
Bean ;
}
}
8. Run the project in tomcat server. There
will be login button visible.
http://localhost:8080/Facebook_Login/
index.jsp
9. Login to Facebook on the Login pup
up.Once you click the login button, It will ask
for user permission.
App permission.
10. After successful login, It will redirect to
Sign_in_Controller.jsp with active access token
in the URL.
Using the active access token Restfb API will get
the user information from the logged in Profile
and print the name.
11. Download the complete project here. p>
FB_LOGIN_PROJECT.zip pre>
Anda baru saja membaca CODE JSP FACEBOOK LOGINJika Ingin Main share lagi tolong sertakan site WAPMILD
Dibuat pada 2018-07-06 23:31:19
Targets jsp code , login fb , script fb
0 Rate upStar