我正在我的网络应用程序上实现facebook登录。
我收到了访问令牌,我知道电子邮件,一切都很好。 但是我无法在Parse数据库上注册用户。
我创建了一个java bean,它有userId,userName,userEmail和token。 我把这些放在一个表格中,应该发送到Parse。
/*--------------------------FORM FACEBOOK ----------------------------*/
final TextField<String> userId = new TextField<>("userId");
final TextField<String> userName = new TextField<>("userName");
final TextField<String> userEmail = new TextField<>("userEmail");
final TextField<String> token = new TextField<>("token");
model = new CompoundPropertyModel<>(new FacebookLoginForm());
form = new Form<FacebookLoginForm>("tokenForm", model)
{
@Override
protected void onSubmit()
{
ParseUser pu = new ParseUser();
pu.setUsername(model.getObject().getUserName());
pu.setEmail(model.getObject().getUserEmail());
/*METHOD FOR PARSE*/
curlParse(model.getObject().getToken(), model.getObject().getUserId());
}
};
add(form);
form.add(userId);
form.add(token);
form.add(userName);
form.add(userEmail);
解析curlParse():
public void curlParse(String token, String id)
{
try
{ Date exp = new Date();
exp.setTime(exp.getTime()+7200000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd'T'HH:MM:ss.SSS'Z'"); /*2012-02-28T23:49:36.353Z Data format*/
String sExp = sdf.format(exp);
Request.Post("https://api.parse.com/1/users")
.addHeader("X-Custom-header", "stuff")
.bodyString(
"{"
+ " \"authData\": { "
+ " \"facebook\": {"
+ " \"id\": \""+ id +"\","
+ "\"access_token\": \""+ token +"\","
+ " \"expiration_date\": \""+sExp+"\""
+ " }"
+ "}"
+ "}", ContentType.APPLICATION_JSON
)
.execute().returnContent().asString();
} catch (IOException ex)
{
Logger.getLogger(Login2.class.getName()).log(Level.SEVERE, null, ex);
}
}
由于我需要通过facebook登录用户的绝对路径,我无法使用调试,日志(Tomcat)文件中没有任何异常。 我试图从cURL转换为java,但它似乎不起作用。 我的错误在哪里?
谢谢。
答案 0 :(得分:0)
on html
<form id="tokenForm" wicket:id="tokenForm" >
<input type="text" id="token" wicket:id="token" name="token"/>
<input type="text" id="userId" wicket:id="userId" name="userId"/>
<input type="text" id="userName" wicket:id="userName"
<input type="text" id="userEmail" wicket:id="userEmail" name="userEmail"/>
<input type="submit"id="facebookSubmit" wicket:id="facebookSubmit" name="facebookSubmit">
</form>
在js:
function login() {
openFB.login(
function (response) {
if (response.status === 'connected') {
getInfo();
document.getElementById("token").value = response.authResponse.token;
} else {
alert('Facebook login failed: ' + response.error);
}
}, {scope: 'email'});
}
function getInfo() {
openFB.api({
path: '/me',
success: function (data) {
console.log(JSON.stringify(data));
document.getElementById("userId").value = data.id;
document.getElementById("userEmail").value = data.email;
document.getElementById("userName").value = data.first_name;
document.getElementById("userSurname").value = data.last_name;
document.getElementById("facebookSubmit").click();
},
error: errorHandler});
}
on java:
public void curlParse(String token, String id)
{
try
{
Date exp = new Date();
exp.setTime(exp.getTime() + 7200000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); /*2012-02-28T23:49:36.353Z */
String sExp = sdf.format(exp);
String resp = Request.Post("https://api.parse.com/1/users")
.addHeader("X-Parse-Application-Id", CWSApplication.getParseDotComApplicationID())
.addHeader("X-Parse-REST-API-Key", CWSApplication.getParseDotComApplicationKey())
.addHeader("Content-Type", "application/json")
.bodyString(
"{"
+ " \"authData\": { "
+ " \"facebook\": {"
+ " \"id\": \"" + id + "\","
+ "\"access_token\": \"" + token + "\","
+ " \"expiration_date\": \"" + sExp + "\""
+ " }"
+ "}"
+ "}", ContentType.APPLICATION_JSON
)
.execute().returnContent().asString();
/*System.out.println(ResponseParse);*/
CWSSession session = (CWSSession) getSession();
ParseUser loggedUser = new ParseUser();
/*questo con la libreriagson ci legge il JSON che ha i dati che ci servono da mettere su Parse*/
try
{
JsonReader reader = new JsonReader(new StringReader(resp));
reader.beginObject();
while (reader.hasNext())
{
String name = reader.nextName();
if (name.equals("objectId"))
{
loggedUser.setObjectId(reader.nextString());
} else if (name.equals("sessionToken"))
{
loggedUser.setSessionToken(reader.nextString());
} else
{
reader.skipValue(); //avoid some unhandle events
}
}
reader.endObject();
reader.close();
} catch (IOException e)
{
e.printStackTrace();
}
}