0

ユーザーのFacebookウォールにアイテムを投稿するJavaWebアプリがあります。ユーザーが最初にサインアップすると、データベースに保持される60日間のaccess_tokenを取得します。これで、offline_accessが削除され、[Loginwithfacebook]ボタンを使用します。ユーザーが当社のWebサイトにログインしたときにトークンを更新するには、通常60日以上離れてアクセスするため、これはすべて問題ありません。

私は上記を実装しましたが、うまく機能しています...しかし、ログインアクションから生成されているアクセストークンは1時間後に期限切れになることがわかりました....明らかに、彼らが離れている間は壁に投稿できません。 。

以下のコードは、signed_requestメソッド(Java SEAMアプリ内)を介してトークンを取得する方法を示しています。これは問題なく機能しますが、トークンは短命です。

トークンが60日タイプであることを確認する方法を誰かが提案できますか

ありがとう

public void loginWithFacebook(){
    accessToken = null;
    try {
        accessToken = FaceBookSecurity.getFBAccessToken();
    } catch (Exception e) {
        log.error("Error getting FB access token: "+e);
    }
    FacebookClient facebookClient = new DefaultFacebookClient(accessToken);
    com.restfb.types.User facebookUser = facebookClient.fetchObject("me", com.restfb.types.User.class);
    facebookEmail = facebookUser.getEmail();
    if (facebookEmail != null) {
        new RunAsOperation(true) {
            public void execute() {
                user = ((UserDAO)Component.getInstance("userDAO")).findByEmail(StringUtils.lowerCase(facebookEmail));
                if (user != null && user.getFacebookToken() != null && !accessToken.equals(user.getFacebookToken())) {
                    user.setFacebookToken(accessToken);
                    log.error("FB: updating "+user.getFirstname()+" "+user.getSurname()+"s FB token to: "+accessToken);
                }
            }
        }.run();
        if (user != null) {
            //set the user as logged in

            return;
        }
    }
    messagePoster.postPopupErrorMessage(messages.get("facebookLoginFailed"));
}

public static String getFBAccessToken()
        throws Exception {

    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    Cookie fbCookie = getFBCookie(request);

    String fbCookieValue = fbCookie.getValue();
    String[] stringArgs = fbCookieValue.split("\\.");
    String encodedPayload = stringArgs[1];
    JsonObject data;
    try{
        String payload = base64UrlDecode(encodedPayload);

        // gets the js object from the cookie
        data = new JsonObject(payload);
    }catch (Exception e){
        return "";
    }

    String authUrl = getAuthURL(data.getString("code"));
    URL url = new URL(authUrl);
    URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(),
            url.getQuery(), null);
    String result = readURL(uri.toURL());

    String[] resultSplited = result.split("&");
    return resultSplited[0].split("=")[1];
}

// creates the url for calling to oauth.
public static String getAuthURL(String authCode) {
    String url = "https://graph.facebook.com/oauth/access_token?client_id="
            + FacebookApp.appId
            + "&redirect_uri=&client_secret="
            + FacebookApp.appSecret + "&code="
            + authCode;
    return url;
}

// reads the url.
private static String readURL(URL url) throws IOException {
    InputStream is = url.openStream();
    InputStreamReader inStreamReader = new InputStreamReader(is);
    BufferedReader reader = new BufferedReader(inStreamReader);
    String s = "";
    int r;
    while ((r = is.read()) != -1) {
        s = reader.readLine();
    }
    reader.close();
    return s;
}

private static String base64UrlDecode(String input){
    return new String(Base64.decodeBase64(input.getBytes()));
}
4

1 に答える 1

1

ユーザーのウォールに投稿するだけでよい場合は、 publish_stream権限を要求していれば、app_access_tokenを使用することもできます。

あなたは呼び出すことができます:

https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID
&client_secret=YOUR_APP_SECRET
&grant_type=client_credentials

これを読んでください

編集:アプリのaccess_tokensは、アプリのシークレットがリセットされるまで期限切れになりません。

于 2012-04-24T15:07:39.857 に答える