用facebook和新浪微博做oauth登入
facebook
參考資料
https://developers.facebook.com/docs/javascript/quickstart/v2.1
<!DOCTYPE html>
<html>
<head>
<title>Facebook Login JavaScript Example</title>
<meta charset="UTF-8">
</head>
<body>
<script>
// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
testAPI();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
document.getElementById('status').innerHTML = 'Please log ' + 'into this app.';
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
document.getElementById('status').innerHTML = 'Please log ' + 'into Facebook.';
}
}
// 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.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxx',
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : 'v2.1' // use version 2.1
});
// Now that we've initialized the JavaScript SDK, we call
// FB.getLoginStatus(). This function gets the state of the
// person visiting this page and can return one of three states to
// the callback you provide. They can be:
//
// 1. Logged into your app ('connected')
// 2. Logged into Facebook, but not your app ('not_authorized')
// 3. Not logged into Facebook and can't tell if they are logged into
// your app or not.
//
// These three cases are handled in the callback function.
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
// Load the SDK asynchronously
( function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id))
return;
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(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.
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.name + '!';
});
}
</script>
<!--
Below we include the Login Button social plugin. This button uses
the JavaScript SDK to present a graphical Login button that triggers
the FB.login() function when clicked.
-->
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();" auto_logout_link="true" show_faces="false"
size="xlarge"></fb:login-button>
<div id="status"></div>
</body>
</html>
請注意:
1 website url必須用.com,.net等符合網站域名的後綴結束。
2 爲了匹配website url,vhost也需要設置跟website一樣的domain
3 //connect.facebook.net/en_US/sdk.js 無法加載,請解決以上2個問題就可以加載了。必須在backend端執行。不能直接訪問下載sdk.js。
login 成功後會返回
authResponse
Object { accessToken="xxx...xxx", userID="xxx", expiresIn=5422, 更多...}
accessToken
"xxxx"
expiresIn
5422
signedRequest
"xxx"
userID
"xxxx"
status
新浪微博
新浪微博要在控制臺中設置callback url和取消callback url。
http://open.weibo.com/apps/698437227/info/advanced
OAuth2.0 授权设置编辑
授权回调页:http://weibo-oauth-local/callback.php
取消授权回调页:http://weibo-oauth-local/cancelCallback.php
下載php sdk。修改config.php
<?php
header('Content-Type: text/html; charset=UTF-8');
define( "WB_AKEY" , 'xxx' );
define( "WB_SKEY" , 'xxx' );
define( "WB_CALLBACK_URL" , 'http://weibo-oauth-local/callback.php' );
WB_CALLBACK_URL要跟在控制台配置的一致。否則無法調用。
<?php
session_start();
include_once( 'config.php' );
include_once( 'saetv2.ex.class.php' );
$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
if (isset($_REQUEST['code'])) {
$keys = array();
$keys['code'] = $_REQUEST['code'];
$keys['redirect_uri'] = WB_CALLBACK_URL;
var_dump($keys);
try {
$token = $o->getAccessToken( 'code', $keys ) ;
var_dump($token);
echo $o->client_id;
} catch (OAuthException $e) {
}
}
if ($token) {
$_SESSION['token'] = $token;
setcookie( 'weibojs_'.$o->client_id, http_build_query($token) );
?>
授权完成,<a href="weibolist.php">进入你的微博列表页面</a><br />
<?php
} else {
?>
授权失败。
<?php
}
?>
回調的php代碼中會返回accesstoken和uid。
分享到:
相关推荐
RT,使用WebView实现新浪微博Oauth2.0认证,非截取html代码获取验证码 界面跳转无副作用
本文档使用C#语言开发,MVC框架调用新浪微博第三方登录OAuth2.0接口。 详细介绍相关开发步骤信息。
新浪微博OAuth2.0API源码 一个更方便,更快速的C#SDK,希望能帮助更多的朋友学习和使用 新浪微博OAuth2.0API使用流程: 1、根据需要求修改配置文件(Wbm.SinaV2.config)。 2、注册ApplicationKey。(参考...
Java,腾讯微博通过OAuth2.0授权,可发文本微博的示例源代码。
新浪腾讯微博oauth2.0登录授权及转发实例
实现新浪微博的OAuth2.0认证登陆,有自己找的资源,以及一些对自己帮助很大的网页,由于新浪微博不支持OAuth1.0了,所以把项目里的新浪微博登陆改为OAuth2.0了
微博Oauth2.0 协议,用java 实现单点登录获取用户信息。
一个更方便,更快速的-新浪微博 OAuth2.0 C# SDK,希望能帮助更多的朋友学习和使用
IOS最新新浪微博开放平台Oauth2.0授权获取Access_Token的Demo
新浪微博oauth认证源码,只要加入开发者自己的key与secret即可通过认证,进行相应的开发
新浪微博C#版SDK Demo 包含获取个人信息、获取会员好友信息、常用的api 分装集成。简单易懂,不管你是高手还是菜鸟都适用。
android 腾讯微博Oauth2.0认证以及自定义webview认证
最近做了新浪微博分享,在网上找了一个 是Oauth1.0的 但是在过不了多少天Oauth1.0 就用不了,就在做了Oauth2.0的图片和文字分享。
对不起,我不得不给这个资源挂上分数,因为该死的CSDN非要折腾个这垃圾功能,我要下载别人的东西没积分没办法....这个里面包括代码,我整理的word笔记文档。 只是我觉得这远远不够,过几天我会录制一个视频,因为我...
asp.net 网易微博 OAuth 2.0 实例源码 1.需要在http://open.t.163.com/ 申请一个帐号 2.获取Appkey,AppSecret 3.需要配置 oauth.config 填入相应的Appkey,AppSecret,原来上面的是我自己乱填的,所以测试需要填你...
【第三方开放平台系列】腾讯微博Oauth2.0授权认证-源码
这是个新浪分享的demo,用的是weiboSDK2.1_130806.jar包,能直接运行,记得修改key的url,还要修改你的应用里的包名。就可以了.
帮助新手学习OAuth的一个很好的示例 http://blog.csdn.net/koastal/article/details/50282509
asp.net 腾讯微博SDK OAuth2.0API接口,示例源码,登陆腾讯微博授权成功后可获得昵称、图像、OpenId,最新微博及粉丝等。100%可用,示例应用于 http://goupiaotong.com/login.aspx
RT,基于OAUTH2.0的腾讯微博认证以及发送微博,代码简洁易懂,方便童鞋们开发需要用到分享信息到腾讯微博功能的应用,具体教程可留意我的CSDN博客http://blog.csdn.net/geniuseoe2012