如何删除facebook中文网在微信第三方授权登陆网站的登陆授权

专业提供ios应用加速审核服务,针对每个首发.更新的APP,提供有效的审核方案,最快速度进入审核状态。(应用和游戏均可,全球任何国家市场均可)&QQ:&&&电话:欢迎咨询
&&国之画&&&&&&
版权所有 京ICP备号-2
迷上了代码!下次自动登录
现在的位置:
& 综合 & 正文
第三方登录开发-Facebook
这次这个项目要分别可以使用新浪微博,qq互联以及Facebook和Twitter授权登录
facebook目前只支持oauth2技术,个人理解其工作流程是当用户想访问当前网站,却不想注册账号,此时当前网站有其它主流网站的第三方登录支持功能,即使用当前站的合作网站的账号密码去第三方合作网站验证用户的合法性,验证成功则允许登录当前站点,无需知道第三方的账号密码。
开发前需要先注册一个facebook的应用,这样可以得到类似密钥的两个值,client_id和client_secret,这两个值是访问facebook url的必要参数;
还要对应用进行一些必要的设置,首先要设置访问facebook和facebook回调的域名,并且配置下白名单,里面为ip值,多个逗号分隔,facebook会校验当前访问的域名解析后对应的ip是否在白名单里面;
接下来注意沙箱选项一定不要启用,否则会获取不到用户的信息,这样facebook应用基本设置完毕,该开始编写请求了。
上面了解了oauth2的工作流程,接下来一起看看其步骤和原理。
当用户点击第三方登录按钮时,会跳转出第三方网站(以下统称为服务端)的登录页面,输入用户名密码后客户端会向服务端申请一个临时令牌(即code值),申请成功后客户端还要申请授权(即token值),如果是第一次访问则会跳出授权页面,用户是否允许访问其一些基本信息,如name、email、用户的Id、图片等基本信息,如果验证及授权通过,则登录当前站点,并且下次会默认之前选择的授权不会弹出提示,除非清除本地cookie。
现在我们来编写代码,首先看下facebook oauth2的几个基本参数和访问url。
基本参数:
1、CLIENT_ID = 602
//为注册应用得到的值
2、redirectUrl = http://blog.csdn.net/webgeek
//为facebook回调当前站点的url
3、SCOPE = "&scope=user_about_me,email,read_stream";
//表示取得的用户信息的权限范围
1、REQUEST_CODE_URL
//发送,得到Authorization Code
/dialog/oauth?" + "client_id="+CLIENT_ID+"&redirect_uri="+redirectUrl+"&response_type=code"+SCOPE;
2、REQUEST_TOKEN_URL
//请求token url
/oauth/access_token
3、REQUEST_USER_URL
//使用得到的token獲取用戶信息
在这并没有使用facebook的官方封装好的工具类,因为有了访问的url(由官方API提供),其实就是发送http请求和接受响应的过程,所以接下来只是编写http的过程。
代码1-controller中:
public void visitUrl(String code, @Param("origURL") String origURL){
if (origURL != null && !"".equals(origURL)) {
this.origUrl = origURL;
//获得响应对象,可按照自己的方式获取
HttpServletResponse response = inv.getResponse();
if (code != null && !"".equals(code)) {
String token = NuomiOauthApi.getFacebookAccessToken(code);
if(token == null || "".equals(token)) {
response.getWriter().write("get code error...");
String[] tokens = token.split("&");
for (String t : tokens) {
if (t.indexOf("access_token") != -1) {
//此处有可能返回的值有多余的数据,可按照开发实际情况来决定是否处理 token = t.split("=")[1].trim();
//此处调用验证通过用户的基本信息 Map&String, String& map = getLoginUser(token);
if (map != null) {
//此处是判断是否本地已经有此用户,是否需要本地注册,可得到用户信息后自行开发 this.isLoginUserOldOrNew(inv, map, origUrl);
response.sendRedirect(INiuxConstants.DOMAIN_TW+origUrl);
////此处是第一次申请code值时
response.sendRedirect(requestCodeUrl);
} catch (Exception e) {
e.printStackTrace();
* 使用Authentication Code方式獲取AcessToken
* @param authCode
如果出錯,返回null
public static String getFacebookAccessToken(String authCode) {
StringBuffer urlStr = new StringBuffer(REQUEST_TOKEN_URL);
urlStr.append('?');
StringBuffer temp = new StringBuffer();
temp.append("client_id=").append(CLIENT_ID).append('&')
.append("redirect_uri=").append(redirectUrl).append('&')
.append("client_secret=").append(CLIENT_SECRET).append('&')
.append("grant_type=authorization_code").append('&')
.append("code=" + authCode);
urlStr.append(temp.toString());
("urlStr: "+urlStr);
String token = getFacebookTokenFromUrl(urlStr.toString(), null);
("get token by authCode: "+token);
} catch (Exception ex) {
log.error("get AccessToken error:" + ex.getMessage(), ex);
* 通過api和token獲取當前登錄的用戶信息
* @param token
* map of id/如果出錯,返回null
public static Map&String, String& getLoginUser(String token) {
StringBuffer urlStr = new StringBuffer(REQUEST_USER_URL);
urlStr.append("?").append("access_token="+encodeUrl(token));
("getLoginUser urlStr:"+urlStr.toString());
JSONObject json = getFacebookJsonFromUrl(urlStr.toString(), null);
("getLoginUser json:"+json);
if (!json.containsKey("id")) {
if (json.containsKey("error_reason")) {
String errorReason = json.getString("error_reason");
String errorDescription = json.getString("error_description");
log.warn("save to facebook failed. errorReason:" + errorReason + ", errorDescription:"+ errorDescription);
Map&String, String& userInfo = new HashMap&String, String&();
userInfo.put("id", json.getString("id"));
userInfo.put("name", json.getString("name"));
userInfo.put("email", json.getString("email"));
userInfo.put("gender", json.getString("gender"));
return userI
} catch (Exception e) {
log.error("get user info failed......", e);
e.printStackTrace();
* 從特定的url中獲取json
* @param urlStr
* @param params
* json object ,or null if failed
private static JSONObject getFacebookJsonFromUrl(String urlStr, Map&String, String& params) {
HttpClient httpClient = new DefaultHttpClient();
//http請求好多都不支持post方式,但支持get
HttpGet httpGet = new HttpGet(urlStr);
JSONObject json =
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
String aStr = IOUtils.toString(is);
json = JSONObject.fromObject(aStr);
} catch (Exception e) {
log.error("http client execute error:" + e.getMessage(), e);
以上就是整个开发的流程,还有几点需要注意:
1、发送请求的url中有个参数为回调url(即代码中的redirectUrl),此参数在请求code和token的时候值必须一致,否则无法得到授权
2、发送的url字符串应该encode一下,浏览器会自动解析
3、发送用户信息请求的时候,一定要用Get方式发送http请求
&&&&推荐文章:
【上篇】【下篇】Android中FaceBook第三方登录集成
1、首先是要先下载 FaceBook SDK Android 版;
2、导入FaceBookSDK 作为Library 引入到自己的项目中
3、在FaceBook 注册一个 测试应用
4、配置AndroidManifest.xml:
在String 中添加:
46645、添加 Facebook 登录按钮 :
6、在activity中添加代码:
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private UiLifecycleHelper uiH
private Session.StatusCallback callback = new Session.StatusCallback() {
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(MainActivity.this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoginButton authButton = (LoginButton) findViewById(R.id.login_button);
authButton.setReadPermissions(Arrays
.asList("email","user_likes", "user_status"));
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
public void onResume() {
super.onResume();
// For scenarios where the main activity is launched and user
// session is not null, the session state change notification
// may not be triggered. Trigger it if it's open/closed.
Session session = Session.getActiveSession();
if (session != null && (session.isOpened() || session.isClosed())) {
onSessionStateChange(session, session.getState(), null);
uiHelper.onResume();
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
public void onPause() {
super.onPause();
uiHelper.onPause();
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
如果想得到用户的基本信息:
private void getUserInfo() {
String fqlQuery = "SELECT uid,name,email FROM user WHERE uid = me()";
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Request request = new Request(Session.getActiveSession(),
HttpMethod.GET,
new Request.Callback(){
public void onCompleted(Response response) {
String str = response.toString();
Log.i(TAG, str);
Request.executeBatchAsync(request);
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'}

我要回帖

更多关于 facebook中文网注册 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信