use of com.codename1.social.Login in project CodenameOne by codenameone.
the class Oauth2 method showAuthentication.
/**
* This method shows an authentication for login form
*
* @param al a listener that will receive at its source either a token for
* the service or an exception in case of a failure
* @return a component that should be displayed to the user in order to
* perform the authentication
*/
public void showAuthentication(ActionListener al) {
final Form old = Display.getInstance().getCurrent();
InfiniteProgress inf = new InfiniteProgress();
final Dialog progress = inf.showInifiniteBlocking();
Form authenticationForm = new Form("Login");
authenticationForm.setScrollable(false);
if (old != null) {
Command cancel = new Command("Cancel") {
public void actionPerformed(ActionEvent ev) {
if (Display.getInstance().getCurrent() == progress) {
progress.dispose();
}
old.showBack();
}
};
if (authenticationForm.getToolbar() != null) {
authenticationForm.getToolbar().addCommandToLeftBar(cancel);
} else {
authenticationForm.addCommand(cancel);
}
authenticationForm.setBackCommand(cancel);
}
authenticationForm.setLayout(new BorderLayout());
authenticationForm.addComponent(BorderLayout.CENTER, createLoginComponent(al, authenticationForm, old, progress));
}
use of com.codename1.social.Login in project CodenameOne by codenameone.
the class FacebookImpl method login.
private void login(final LoginCallback cb) {
if (loginLock) {
return;
}
loginLock = true;
LoginManager login = LoginManager.getInstance();
final CallbackManager mCallbackManager = CallbackManager.Factory.create();
if (AndroidNativeUtil.getActivity() == null) {
throw new RuntimeException("Cannot login to facebook when running in the background.");
}
final CodenameOneActivity activity = (CodenameOneActivity) AndroidNativeUtil.getActivity();
activity.setIntentResultListener(new IntentResultListener() {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
mCallbackManager.onActivityResult(requestCode, resultCode, data);
activity.restoreIntentResultListener();
}
});
login.registerCallback(mCallbackManager, new FBCallback(cb));
login.logInWithReadPermissions(activity, permissions);
}
use of com.codename1.social.Login in project CodenameOne by codenameone.
the class FacebookImpl method askPublishPermissions.
public void askPublishPermissions(final LoginCallback cb) {
if (AndroidNativeUtil.getActivity() == null) {
throw new RuntimeException("Cannot ask for publish permissions when running in the background.");
}
if (loginLock) {
return;
}
loginLock = true;
LoginManager login = LoginManager.getInstance();
final CallbackManager mCallbackManager = CallbackManager.Factory.create();
final CodenameOneActivity activity = (CodenameOneActivity) AndroidNativeUtil.getActivity();
activity.setIntentResultListener(new IntentResultListener() {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
mCallbackManager.onActivityResult(requestCode, resultCode, data);
activity.restoreIntentResultListener();
}
});
login.registerCallback(mCallbackManager, new FBCallback(cb));
login.logInWithPublishPermissions(activity, PUBLISH_PERMISSIONS);
}
use of com.codename1.social.Login in project CodenameOne by codenameone.
the class GoogleImpl method nativeLoginImpl.
private void nativeLoginImpl(final GoogleApiClient client) {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(client);
AndroidNativeUtil.startActivityForResult(signInIntent, RC_SIGN_IN, new IntentResultListener() {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
final GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
String displayName = acct.getDisplayName();
String acctId = acct.getId();
String email = acct.getEmail();
String requestIdToken = acct.getIdToken();
Set<Scope> grantedScopes = acct.getGrantedScopes();
String code = acct.getServerAuthCode();
String scopeStr = scope;
System.out.println("Token is " + acct.getIdToken());
if (acct.getIdToken() == null && clientId != null && clientSecret != null) {
Log.p("Received null ID token even though clientId and clientSecret are set.");
}
// otherwise we'll set the token to null.
if (clientId != null && clientSecret != null && requestIdToken != null && code != null) {
ConnectionRequest req = new ConnectionRequest() {
@Override
protected void readResponse(InputStream input) throws IOException {
Map<String, Object> json = new JSONParser().parseJSON(new InputStreamReader(input, "UTF-8"));
if (json.containsKey("access_token")) {
setAccessToken(new AccessToken((String) json.get("access_token"), null));
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
callback.loginSuccessful();
}
});
} else {
setAccessToken(new AccessToken(null, null));
Log.p("Failed to retrieve the access token from the google auth server. Login succeeded, but access token is null, so you won't be able to use it to retrieve additional information.");
Log.p("Response was " + json);
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
callback.loginSuccessful();
}
});
}
}
};
req.setUrl("https://www.googleapis.com/oauth2/v4/token");
req.addArgument("grant_type", "authorization_code");
// req.addArgument("client_id", "555462747934-iujpd5saj4pjpibo7c6r9tbjfef22rh1.apps.googleusercontent.com");
req.addArgument("client_id", clientId);
// req.addArgument("client_secret", "650YqplrnAI0KXb9LMUnVNnx");
req.addArgument("client_secret", clientSecret);
req.addArgument("redirect_uri", "");
req.addArgument("code", code);
req.addArgument("id_token", requestIdToken);
req.setPost(true);
req.setReadResponseForErrors(true);
NetworkManager.getInstance().addToQueue(req);
} else {
setAccessToken(new AccessToken(null, null));
Log.p("The access token was set to null because one of clientId, clientSecret, requestIdToken, or auth were null");
Log.p("The login succeeded, but you won't be able to make any requests to Google's REST apis using the login token.");
Log.p("In order to obtain a token that can be used with Google's REST APIs, you need to set the clientId, and clientSecret of" + "the GoogleConnect instance to valid OAuth2.0 Client IDs for Web Clients.");
Log.p("See https://console.developers.google.com/apis/credentials");
Log.p("You can get the OAuth2.0 client ID for this project in your google-services.json file in the oauth_client section");
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
callback.loginSuccessful();
}
});
}
} else {
if (callback != null) {
if (callback != null) {
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
callback.loginFailed(GooglePlayServicesUtil.getErrorString(result.getStatus().getStatusCode()));
}
});
}
}
}
}
}
});
}
use of com.codename1.social.Login in project CodenameOne by codenameone.
the class CloudPersona method createOrLogin.
/**
* Creates a new user if a user isn't occupying the given login already,
* if the user exists performs a login operation.
*
* @param login a user name
* @param password a password
* @return true if the login is successful false otherwise
*/
public static boolean createOrLogin(String login, String password) {
if (instance == null) {
getCurrentPersona();
if (instance.persona != null) {
return true;
}
}
ConnectionRequest loginRequest = new ConnectionRequest();
loginRequest.setPost(true);
loginRequest.setUrl(CloudStorage.SERVER_URL + "/objStoreUser");
loginRequest.addArgument("l", login);
loginRequest.addArgument("p", password);
loginRequest.addArgument("pk", Display.getInstance().getProperty("package_name", null));
loginRequest.addArgument("bb", Display.getInstance().getProperty("built_by_user", null));
NetworkManager.getInstance().addToQueueAndWait(loginRequest);
if (loginRequest.getResposeCode() != 200) {
return false;
}
ByteArrayInputStream bi = new ByteArrayInputStream(loginRequest.getResponseData());
DataInputStream di = new DataInputStream(bi);
try {
if (di.readBoolean()) {
if (instance == null) {
instance = new CloudPersona();
}
instance.persona = di.readUTF();
Preferences.set("CN1Persona", instance.persona);
Util.cleanup(di);
} else {
Util.cleanup(di);
return false;
}
} catch (IOException ex) {
ex.printStackTrace();
}
return true;
}
Aggregations