您好,我的脚本与之前发布的facebook API运行良好。 使用oauth 2.0并不是更有效。 我解决了登录的第一个问题,现在我得到了我的专辑列表但是我没有这些照片。
此第一个脚本进行授权并检索专辑列表。 当我选择一个相册时,我将id和token传递给第二个脚本中的php文件。在那里我应该加载指定专辑的图片,但不起作用。
我想知道问题是否在封面电话https://graph.facebook.com/'+album+'/picture?access_token='+access_token+
并在
FB.api("/"+albumid+"/photos",function(response){…
呼叫..
Saomeone能帮助我找到我错的地方吗?
这里可以找到完整的脚本。 goo.gl/HY59u 感谢
费德里科
<div id="fb-root"></div>
<script>
var loggedIn = false;
var isLoaded = false;
function loginFacebook() {
if(!isLoaded) {
alert("SDK is not yet loaded or something went wrong!");
return false;
}
//initializes the facebook API
document.getElementById("status").innerHTML = "In attesa del permesso Facebook";
//opens the Facebook login window for user
FB.login(function(response) {
if (response.authResponse) {
document.getElementById("status").innerHTML = "Logged In. Ora puoi caricare gli album.";
loggedIn = true;
//disables the login button after the user has loggedIn
document.getElementById("loginBtn").disabled = "Disabilitato";
document.getElementById("loginBtn").style.display = "None";
document.getElementById("albumBtn").style.display = "inline";
document.getElementById("fb_logo").style.display = "None";
document.getElementById("label_fb").style.display = "None";
} else {
document.getElementById("status").innerHTML = "Non hai effettuato il Facebook Login";
loggedIn = false;
}
},{scope:'user_photos'});
}
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxx', // App ID
channelUrl : 'www.xxxxx.com/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional initialization code here
isLoaded = true;
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
//loads the all the album currently present in user's facebook profile
//Requests to '/me/albums'
function loadAlbums() {
if(loggedIn) {
document.getElementById("status").innerHTML = "Caricando gli album dal tuo profilo Facebook";
var counter = 0;
FB.api('/me/albums', function(response) {
document.getElementById("albumsList").innerHTML = '';
for(var c in response["data"]) {
addOption(response["data"][c].name,response["data"][c].id);
counter++;
}
document.getElementById("albumBtn").style.display = "none";
document.getElementById("status").innerHTML = "<p>Ci sono "+ counter +" album sul tuo profilo Facebook.</p><p> Scegli quello che vuoi inserire</p>";
});
} else {
document.getElementById("status").innerHTML = "Non hai effettuato il Facebook Login. Fai il Facebook login per caricare gli album.";
}
}
//Adds a new album link into albumsList div
function addOption(opText,opVal) {
document.getElementById("albumsList").style.display = "block";
var v = document.getElementById("albumsList");
v.innerHTML += '<br/><a target="_self" href="../facebook/album.php?id='+opVal+'&name='+opText+'">'+opText+'</a>';
}
function init() {
var v = document.getElementById("loginBtn");
v.onclick = loginFacebook;
v.disabled = "";
var v1 = document.getElementById("albumBtn");
v1.onclick = loadAlbums;
v1.disabled = "";
}
//calls init function once all the resources are loaded
window.addEventListener("load",init,true);
其他剧本
<!-- Include the Facebook Javascript API -->
<script src="http://connect.facebook.net/en_US/all.js"></script>
<!-- Include the normal stylesheet-->
<link href="style.css" rel="stylesheet" />
<script type="text/javascript">
var album = "";
var access_token = "";
function init() {
FB.init({
appId : 'xxxxxxx', // App ID
channelUrl : 'www.sssss.com/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
var _album = getParameter('id');
if(_album!="") {
album = _album;
access_token = getAccessToken();
document.getElementById("album_desc").innerHTML = unescape(getParameter('name'));
//Album Header Photo link, https://graph.facebook.com/ALBUM_ID/picture?access_token = ACCESS_TOKEN
document.getElementById("album_cover").innerHTML += '<img src="https://graph.facebook.com/'+album+'/picture?access_token='+access_token+'" />';
getAlbumPhotos(album);
} else {
album = "No Album ID passed";
document.getElementById("album_cover").innerHTML = '<span class="error">'+album+'</span>';
}
}
//queries the cookie on browser, to get the access_token
function getAccessToken() {
splitArray = document.cookie.split(";");
access_token = "";
for(var v in splitArray) {
if(splitArray[v].substr(0,4)==" fbs") {
access_token = splitArray[v];
break;
}
}
var arr = access_token.split("&")[0].split("=");
return arr[arr.length-1];
}
//gets all the photos in the album
function getAlbumPhotos(albumid) {
//Queries /ALBUM_ID/photos
FB.api("/"+albumid+"/photos",function(response){
var photos = response["data"];
document.getElementById("photos_header").innerHTML = "Foto("+photos.length+")";
for(var v=0;v<photos.length;v++) {
var image_arr = photos[v]["images"];
var subImages_text1 = "("+(v+1)+")";
//this is for the small picture that comes in the second column
var subImages_text2 = '<img src="'+image_arr[(image_arr.length-1)]["source"]+'" class="imagedropshadow" />';
//this is for the third column, which holds the links other size versions of a picture
var subImages_text3 = "";
//gets all the different sizes available for a given image
for(var j = 0 ;j<image_arr.length;j++) {
var Maxsize = Math.max(image_arr[0]["width"]);
if(image_arr[j]["width"]== Maxsize)
{
subImages_text3 += '<a target="_blank" href="'+image_arr[j]["source"]+'">Seleziona</a> <input type="checkbox" name="checkbox_fb[]" value="'+image_arr[j]["source"]+'"> <input type="hidden" name="w_prev" id="w_prev" value="'+image_arr[(image_arr.length-1)]["width"]+'"/> <input type="hidden" name="h_prev" id="h_prev" value="'+image_arr[(image_arr.length-1)]["height"]+'"/> <br/>';
}
}
addNewRow(subImages_text1,subImages_text2,subImages_text3);
}
});
}
function addNewRow(data1,data2,data3) {
//inserts a new row into the table
var table = document.getElementById("album_photos");
var row = table.insertRow(table.rows.length);
var cell = row.insertCell(0);
cell.innerHTML = data1;
cell = row.insertCell(1);
cell.innerHTML = data2;
cell = row.insertCell(2);
cell.innerHTML = data3;
}
//This function gets the value of album, passed in the request string
function getParameter(name)
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
//the regular expression that separates the album from the queryString
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
//calls init function once all the resources are loaded
window.addEventListener("load",init,true);
</script>