如何以编程方式发送Facebook群组邀请

时间:2013-05-08 10:23:41

标签: facebook facebook-graph-api facebook-fql facebook-group

如果用户尚未登录Facebook,我想创建一个可以提示登录的页面,然后检查用户是否是某个组的成员。如果没有,请提供一个按钮,单击该按钮将向该用户发送群组邀请。

在发送群组邀请的最后一步之前,我可以做所有事情。我想知道它是否有可能。该小组是一个普通的开放小组,而不是文档所说的仍处于测试阶段的应用小组。

我console.log响应,看到单击按钮时出现以下错误。

Object {error: Object}
error: Object
code: 3
message: "(#3) "
type: "OAuthException"

错误代码和错误消息根本没有帮助...

这是我到目前为止的完整代码

<html>
<head>
    <title>Group Tester</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="css/style1.css" />
    <script src="jquery/jquery-1.9.1.min.js"></script>
    <style>
        body.connected #login { display: none; }
        body.connected #logout { display: block; }
        body.not_connected #login { display: block; }
        body.not_connected #logout { display: none; }
    </style>
</head>
<body onload="setTimeout(function() { window.scrollTo(0, 1) }, 100);"></body>
<div id="fb-root"></div>
<script>
  (function() {
    var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
        }());
</script>
<script>
var uid = "";
var fbName = "";
var accessToken = "";
var appId = "xxxxxxxxxxxxxx";
var appSecret = "xxxxxxxxxxxxxxxxxxxxxx";
var appAccessToken = "";
var groupId = "xxxxxxxxxxxxxxxxxx";

$(document).ready(function(){
    $("#group").hide();
    window.fbAsyncInit = function() {
        FB.init({ appId: appId, 
            status: true, 
            cookie: true,
            xfbml: true,
            oauth: true});

        FB.Event.subscribe('auth.authResponseChange', handleResponseChange); 
        checkLoginStatus();
    };
});
function handleResponseChange(response) {
    document.body.className = response.authResponse ? 'connected' : 'not_connected';

    if (response.authResponse) {
        checkLoginStatus();
    }
}

function loginUser() {    
    FB.login(function(response) { }, {scope:'email, user_likes, manage_groups, user_groups'});     
}

function checkLoginStatus() {
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            // the user is logged in and has authenticated your
            // app, and response.authResponse supplies
            // the user's ID, a valid access token, a signed
            // request, and the time the access token 
            // and signed request each expire
            document.body.className = 'connected';
            uid = response.authResponse.userID;
            accessToken = response.authResponse.accessToken;
            FB.api('/me', function(response) {
                loginSuccess(response);
            });
        } else {
            // the user isn't logged in to Facebook.
            notLoggedIn();
        }
    });
}

function checkGroupMember()
{
    var fql = "SELECT uid from group_member WHERE uid=me() AND gid='"+groupId+"'";
    var fqlencoded = encodeURIComponent(fql);
    FB.api("/fql?q="+fqlencoded, 
        function(response) {
            //console.log(response.data[0].uid);
            if (response.data.length != 0)
            {
                $("#sendInvite").hide();
                $("#alreadyMember").show();
            }
            else
            {
                $("#sendInvite").show();
                $("#alreadyMember").hide();
            }
        }
    );
}

function logoutUser() {
    FB.logout();
    notLoggedIn();
}

function loginSuccess(response) {
    $("#profileName").html(response.name);
    fbName = response.name;
    $("#profileImg").html("<img src='https://graph.facebook.com/"+uid+"/picture?type=large' />");
    $("#group").show();
    console.log("logged in");
    checkGroupMember();
}

function notLoggedIn() {
    document.body.className = 'not_connected';
    $("#profileImg").html("");
    $("#profileName").html("");
    $("#group").hide();
}

function sendInvite()
{
    var connection = '/'+groupId+'/members/'+uid;
    console.log(connection);
    console.log(accessToken);
    FB.api(connection, 'post', {access_token : accessToken}, function(response) {
        console.log(response);
    });
}

</script>
 <div id="wrapper">
    <center><h1>Group API Test</h1></center>
    <div id="login" onClick="loginUser();"></div>
    <div id="logout" onClick="logoutUser();"></div>
    <br />
    <div id="group">
        <div id="profileImg"></div>
        <div id="profileName"></div>
        <div id="sendInvite" style="display:none;" onclick="sendInvite()">Send Invite Now</div>
        <div id="alreadyMember" style="display:none;">Already a Member</div>
        <div id="message"></div>
    </div>
</div>
</body>
</html>

身份验证流程运行正常,问题出在sendInvite()函数中,我愿意接受建议。

哦......,如果有人想要这个代码的css文件。 (这不是问题的关键)

body {
    text-align: center; 
    font-family:Comic Sans MS;
    text-shadow: 0 1px 0 #222222;
}

h1 {
    color: #34D13C;
    margin-top:20px;
    font-weight: bold;
    text-shadow: 0 3px 0 #222222;
}

#login
{
    width: 413px;
    height: 70px;
    background-image:url('../img/login.png');
    margin-left:auto;
    margin-right:auto;
    display:none;
    cursor:pointer;
}

#logout
{
    width: 413px;
    height: 70px;
    background-image:url('../img/logout.png');
    margin-left:auto;
    margin-right:auto;
    display:none;
    cursor:pointer;
}

#sendInvite
{
    background-color: cyan;
    cursor: pointer;
    border: black thin solid;
    margin-top:10px;
    max-width: 200px;
    padding: 5px;
    margin-left:auto;
    margin-right:auto;
}

#sendInvite:hover
{
    background-color: skyblue;
    color : green;
}

0 个答案:

没有答案