Parse.com obsługuje tylko Facebook i Twitter, ale jeszcze nie google +, za który trzeba zaimplementować własną uwierzytelniania przy użyciu w chmurze (parse.com) bocznej
To jest długi proces, więc bądź cierpliwy
Śledź ten kroki
1) uzyskać informacje o profilu google za to zaimplementować to niezbędne funkcje
wprowadzenia 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) sposobu realizowanego 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) połączenia w google przycisku + kliknięcie
private void loginUsingGoolgePlus() {
// TODO Auto-generated method stub
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
4) Metody wdrożonego C onnectionCallbacks
@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) metoda ta daje informacje o profilu
/**
* 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) to ważne tutaj możemy wygenerować jeden znak i za pomocą tego i e-mail id, nazywamy skrypt funkcja accessGoogleUser
po stronie chmury (Parse.com) Kod cloud (to nic, ale javascript main.js
- accessGoogleUser
da Ci accessToken użyciu tego accessToken można zrobić, zaloguj się lub zarejestruj
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) Jak przesłać kod cloudmain.js
w przetworzenia. com chmura
uważnie przeczytać i pobrać Parse.exe iz here po pobraniu zrobić
place parse.exe and ParseConsole.exe in ':\Windows\System32' folder. Then search for Windows PowerShell in the start menu and run it as an administrator. Wait for the prompt in the window (mine was a blue window) to indicate it is in the ':\Windows\System32' folder. Then type '.\ParseConsole.exe' and press enter.
ten sposób możemy przesyłać pliki
poniżej pliki będą tworzyć w C: \ Users \ xxxx natomiast śledzić
1) cloud
- main.js
2) config
─ global.json
3) public
─ index.html
8) pobierz główny. js z here i zamień na domyślny main.js
który powstał w cloud folder
Uwaga: nie zapomnieli dodać swój identyfikator klienta i tajemnicy w tej main.js
9) sprawdzić to zbyt !!.
Require Revocable Sessions should be false
w przeglądarce parsowania danych -> Ustawienia -> Ogólne
Zapytaj mnie za wszelką wątpliwość. jestem gotowy na pomoc. !!
Dobra robota! Działa świetnie. Moim jedynym problemem jest próba użycia kodu CloudCode przez Rest Api. Użytkownik jest tworzony i otrzymuję token sesji w pierwszej próbie. Ale gdy użytkownik jest już utworzony, nie otrzymuję tokena sesji, tylko pustą odpowiedź. Każdy pomysł, dlaczego? –
zrobiłeś 9 punkt? –
Nieodebrane. Działa teraz idealnie. Tks dużo kishore –