restFB生成的令牌是真的吗?

时间:2016-11-02 18:02:50

标签: java facebook-graph-api restfb

我使用restFB调用FaceBook Graph api登录,获取用户的帖子等。

在Facebook的开发者帐户中创建了一个应用程序。

尝试使用此处生成的令牌调用用户详细信息, https://developers.facebook.com/tools/explorer?method=GET&path=8560751784547897&version=v2.8

例如:来自上面网址的accessToken提供了= EAA....F

当我在浏览器中点击url时,我能够获得所有用户详细信息,如下所示, https://graph.facebook.com/v2.8/me?fields=id,name,email,birthday?access_token=EAA....f

我在浏览器中收到以下回复,

{
   "id": "1127949",
   "name": "youtr name",
   "email": "youmail\u0040gmail.com"
   "birthday": "10/27/1998"
}

我使用以下代码动态生成了accessstoken,

        StringBuffer callbackURLbuffer = request.getRequestURL();
        int index = callbackURLbuffer.lastIndexOf("/");
        callbackURLbuffer.replace(index, callbackURLbuffer.length(), "").append("/callback");
        callbackURL = URLEncoder.encode(callbackURLbuffer.toString(), "UTF-8");

        String authURL = "https://graph.facebook.com/oauth/authorize?client_id="
                + facebookAppId
                + "&redirect_uri="
                + callbackURL
                + "&scope=user_about_me,"
                + "user_actions.books,user_actions.fitness,user_actions.music,user_actions.news,user_actions.video,user_activities,user_birthday,user_education_history,"
                + "user_events,user_photos,user_friends,user_games_activity,user_groups,user_hometown,user_interests,user_likes,user_location,user_photos,user_relationship_details,"
                + "user_relationships,user_religion_politics,user_status,user_tagged_places,user_videos,user_website,user_work_history,ads_management,ads_read,email,"
                + "manage_notifications,manage_pages,publish_actions,read_friendlists,read_insights,read_mailbox,read_page_mailboxes,read_stream,rsvp_event";

在我的callbackURL servlet中,我获得了code值的accessToken,如下所示,

        StringBuffer redirectURLbuffer = request.getRequestURL();
        int index = redirectURLbuffer.lastIndexOf("/");
        redirectURLbuffer.replace(index, redirectURLbuffer.length(), "").append("/callback");
        redirectURL = URLEncoder.encode(redirectURLbuffer.toString(), "UTF-8");

        code = request.getParameter("code");
        if(null!=code) {
            accessURL = "https://graph.facebook.com/oauth/access_token?client_id=" + facebookAppId + 
                    "&redirect_uri=" + redirectURL + "&client_secret=" + facebookAppSecret + "&code=" + code;
            webContent = getWebContentFromURL(accessURL);
            accessToken = getAccessTokenFromWebContent(webContent);

上述代码使用的其他函数

private static String getWebContentFromURL(String webnames) {
    try {
        URL url = new URL(webnames);
        URLConnection urlc = url.openConnection();
        //BufferedInputStream buffer = new BufferedInputStream(urlc.getInputStream());
        BufferedReader buffer = new BufferedReader(new InputStreamReader(urlc.getInputStream(), "UTF8"));
        StringBuffer builder = new StringBuffer();
        int byteRead;
        while ((byteRead = buffer.read()) != -1)
            builder.append((char) byteRead);
        buffer.close();
        String text=builder.toString();
        return text;
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

private static String getAccessTokenFromWebContent (String webContent) {
    String accessToken = null;
    int s = webContent.indexOf("access_token=") + ("access_token=".length());
    int e = webContent.indexOf("&");
    accessToken = webContent.substring(s, e);
    return accessToken;
}

让我们说这个accessToken = EAA....2

如果我使用此令牌调用以下网址, https://graph.facebook.com/v2.8/me?fields=id,name,email,birthday&access_token=EAA....2

我收到了以下回复,

{
   "id": "1127949",
   "name": "youtr name",
   "email": "youmail\u0040gmail.com"
}

如果我点击跟随url,使用bio feild,我在json中收到错误信息如下,

{
  "error": {
  "message": "(#12) bio field is deprecated for versions v2.8 and higher",
  "type": "OAuthException",
  "code": 12,
  "fbtrace_id": "ARkoFJVP/Jk"
   }
}

有人可以说为什么会发生这种情况。

1 个答案:

答案 0 :(得分:0)

看看这里,了解用户权限。

https://developers.facebook.com/docs/facebook-login/permissions/#reference-user_about_me

根据您的评论,关于调试您的令牌,听起来好像您没有获得Facebook为library(plyr) df.a <- c(5, 4, 5, 7, 3, 5, 6, 5, 5, 4, 5, 5, 4, 5, 4, 7, 2, 4, 4, 5, 3, 6, 5, 6, 4, 4, 5, 4, 5, 5, 6, 7, 4) df.b <- c(1, 3, 4, 6, 2, 7, 7, 4, 3, 6, 6, 3, 6, 6, 5, 6, 6, 5) df.c <- c(3, 1, 3, 6, 6, 5, 7, 6, 6, 2, 7, 5, 1) table(df.a) count(df.a) df.a.count <- count(df.a) df.b.count <- count(df.b) df.c.count <- count(df.c) #normalize the data df.a.count$freq <- sapply(df.a.count$freq, function(X) X/length(df.a)) df.b.count$freq <- sapply(df.b.count$freq, function(X) X/length(df.b)) df.c.count$freq <- sapply(df.c.count$freq, function(X) X/length(df.c)) #solution using merge df.m <- merge(df.a.count, df.b.count, by ='x', all=TRUE) df.m <- merge(df.m, df.c.count, by ='x', all=TRUE)[2:4] names(df.m) <- c('freq.a', 'freq.b','freq.c') #problem using join_all dfs <- list(df.a.count, df.b.count, df.c.count) df.all <- join_all(dfs, 'x') 启用的权限。

您必须向Facebook发送有关您的应用的评论,并需要通过Facebook批准,然后才能访问用户详细信息和其他内容。

请按照以下步骤操作

  1. 转到 - https://developers.facebook.com/apps/your_app_id/review-status/

  2. 点击开始提交按钮。

  3. 选中您需要的权限复选框。然后单击“添加项目”按钮。

  4. 然后点击每个权限的编辑备注。

  5. 在这里,有关此权限的应用程序的breif,并上传您的应用程序的演示视频。

  6. Facebook会根据规范对其进行审核并接受/拒绝。

  7. 这是你缺少的,它会导致你的问题..