我知道有人问过这个问题,但我找不到任何解决方案。我正在使用 Parse,用户可以在其中使用 Facebook、Twitter 和 Google+ 登录。截至目前,只有 Facebook 和 Twitter 功能齐全。
我已成功通过以下方式使用 Facebook 和 Twitter 登录:
private void onLoginButtonClicked() {
LoginActivity.this.progressDialog = ProgressDialog.show(
LoginActivity.this, "", "Logging in...", true);
List<String> permissions = Arrays.asList("public_profile", "user_about_me",
"user_relationships", "user_birthday", "user_location");
ParseFacebookUtils.logIn(permissions, this, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException err) {
LoginActivity.this.progressDialog.dismiss();
if (user == null) {
Log.d(IntegratingFacebookTutorialApplication.TAG,
"Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.d(IntegratingFacebookTutorialApplication.TAG,
"User signed up and logged in through Facebook!");
showUserDetailsActivity();
} else {
Log.d(IntegratingFacebookTutorialApplication.TAG,
"User logged in through Facebook!");
moodpage();
}
}
});
}
private void onTwitterButtonClicked() {
ParseTwitterUtils.logIn(this, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
Log.d("MyApp", "Uh oh. The user cancelled the Twitter login.");
} else if (user.isNew()) {
Log.d("MyApp", "User signed up and logged in through Twitter!");
showUserDetailsActivity();
} else {
Log.d("MyApp", "User logged in through Twitter!");
moodpage(); }
}
});
}
我正试图通过解析与 Google+ 一起实现这一目标。有人建议我研究 Parse Rest API,但是,我不熟悉它,需要更多指导。
有些人建议我使用 https://github.com/Glamdring/google-plus-java-api/看起来很有前途,但我不确定我将如何解决这个问题。
例如假设我有
googleButton = (Button) findViewById(R.id.twitterButton);
googleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onGoogleButtonClicked();
}
});
private void onGoogleButtonClicked(); {
//what to input here
}
如有任何澄清,我们将不胜感激。
最佳答案
Parse.com 仅支持 facebook 和 twitter 但不支持 google+,因为你必须在云端 (parse.com) 端实现你自己的身份验证
这是一个漫长的过程,所以请耐心等待
按照以下步骤操作
1) 获取 实现此必要功能
的 google 个人资料信息放入onCreate()
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this) //lets impement ConnectionCallbacks
.addOnConnectionFailedListener(this).addApi(Plus.API) // lets implement OnConnectionFailedListener
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
mGoogleApiClient.connect();
2)实现的OnConnectionFailedListener方法
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), getActivity(),
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
}
/**
* Method to resolve any signin errors for google plus
* */
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(getActivity(), RC_SIGN_IN);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
3) 调用 google+ 按钮点击
private void loginUsingGoolgePlus() {
// TODO Auto-generated method stub
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
4) 已实现的 ConnectionCallbacks 方法
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
mSignInClicked = false;
Toast.makeText(getActivity(), "User is connected!", Toast.LENGTH_LONG).show();
// Get user's information
getProfileInformation();
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
mGoogleApiClient.connect();
}
5) 此方法将为您提供个人资料信息
/**
* Fetching user's information name, email, profile pic
* */
private void getProfileInformation() {
try {
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi
.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String personPhotoUrl = currentPerson.getImage().getUrl();
String personGooglePlusProfile = currentPerson.getUrl();
final String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
Log.e(TAG, "Name: " + personName + ", plusProfile: "
+ personGooglePlusProfile + ", email: " + email
+ ", Image: " + personPhotoUrl);
// by default the profile url gives 50x50 px image only
// we can replace the value with whatever dimension we want by
// replacing sz=X
personPhotoUrl = personPhotoUrl.substring(0,
personPhotoUrl.length() - 2)
+ PROFILE_PIC_SIZE;
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
googleAuthWithParse(email);
}
}).start();
} else {
Toast.makeText(getActivity(),
"Person information is null", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
6) 这很重要
在这里我们生成一个 token 并使用它和电子邮件 ID,我们在云端 (Parse.com) 调用脚本函数 accessGoogleUser
(这个云代码只是 javascript main.js
-accessGoogleUser
将使用此 accessToken 为您提供 accessToken,您可以进行登录或注册
protected void googleAuthWithParse(String email) {
// TODO Auto-generated method stub
String scopes = "oauth2:" + Scopes.PLUS_LOGIN + " ";
String googleAuthCode = null;
try {
googleAuthCode = GoogleAuthUtil.getToken(
getActivity(), // Context context
email, // String email
scopes, // String scope
null // Bundle bundle
);
} catch (UserRecoverableAuthException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (GoogleAuthException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Log.i(TAG, "Authentication Code: " + googleAuthCode);
final HashMap<String, Object> params = new HashMap<String, Object>();
params.put("code", googleAuthCode);
params.put("email", email);
//loads the Cloud function to create a Google user
ParseCloud.callFunctionInBackground("accessGoogleUser", params, new FunctionCallback<Object>() {
@Override
public void done(Object returnObj, ParseException e) {
if (e == null) {
Log.e("AccessToken", returnObj.toString());
ParseUser.becomeInBackground(returnObj.toString(), new LogInCallback() {
public void done(final ParseUser user, ParseException e) {
if (user != null && e == null) {
showToast("The Google user validated");
if(user.isNew()){
//isNew means firsttime
}else{
loginSuccess();
}
} else if (e != null) {
showToast("There was a problem creating your account.");
e.printStackTrace();
mGoogleApiClient.disconnect();
} else
showToast("The Google token could not be validated");
}
});
} else {
if (e != null) {
try {
JSONObject jsonObject = new JSONObject(e.getMessage());
showToast(jsonObject.getString("message"));
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
mGoogleApiClient.disconnect();
}
}
}
});
}
7) 如何上传云端代码 main.js
at parse.com cloud
仔细阅读并下载 Parse.exe 并从 here下载后做
将 parse.exe 和 ParseConsole.exe 放在“:\Windows\System32”文件夹中。然后在开始菜单中搜索Windows PowerShell,以管理员身份运行。等待窗口中的提示(我的是蓝色窗口)表明它在 ':\Windows\System32' 文件夹中。然后键入“.\ParseConsole.exe”并按回车键。
这是我们上传文件的方式
以下文件将在 C:\Users\xxxx 中创建,同时遵循 图像步骤
1) cloud
- main.js
2) config
─ global.json
3) public
─ index.html
8) 从 here 下载 main.js并替换为在 cloud folder
main.js
注意:不要忘记将您的客户端 ID 和密码添加到此 main.js
9) 也检查这个。!!
Require Revocable Sessions should be false
在解析数据浏览器 -> 设置中
->一般
有任何疑问都可以问我。我准备好寻求帮助了。!!
关于java - 在 Parse 上使用 Google+ 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26440729/