Search in sources :

Example 6 with Login

use of com.codename1.social.Login in project CodenameOne by codenameone.

the class CloudPersona method createAnonymous.

/**
 * Creates an anonymous persona that will be unique in the cloud, NEVER logout an anonymous user!
 * @return false in case login failed e.g. due to bad network connection
 */
public static boolean createAnonymous() {
    if (instance == null) {
        getCurrentPersona();
    }
    ConnectionRequest login = new ConnectionRequest();
    login.setPost(true);
    login.setUrl(CloudStorage.SERVER_URL + "/objStoreUser");
    login.addArgument("pk", Display.getInstance().getProperty("package_name", null));
    login.addArgument("bb", Display.getInstance().getProperty("built_by_user", null));
    NetworkManager.getInstance().addToQueueAndWait(login);
    if (login.getResposeCode() != 200) {
        return false;
    }
    ByteArrayInputStream bi = new ByteArrayInputStream(login.getResponseData());
    DataInputStream di = new DataInputStream(bi);
    if (instance == null) {
        instance = new CloudPersona();
    }
    try {
        instance.persona = di.readUTF();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    Preferences.set("CN1Persona", instance.persona);
    Preferences.set("CN1PersonaAnonymous", true);
    Util.cleanup(di);
    return true;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 7 with Login

use of com.codename1.social.Login in project CodenameOne by codenameone.

the class SignIn method start.

public void start() {
    // new SignInFormGB(theme).show();
    // if (true) return;
    loginForm = new Form("Sign in Demo");
    loginForm.setLayout(new BorderLayout());
    Container center = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    center.setUIID("ContainerWithPadding");
    Image logo = theme.getImage("CodenameOne.png");
    Label l = new Label(logo);
    Container flow = new Container(new FlowLayout(Component.CENTER));
    flow.addComponent(l);
    center.addComponent(flow);
    final TextField username = new TextField();
    username.setHint("Username");
    final TextField pass = new TextField();
    pass.setHint("Password");
    pass.setConstraint(TextField.PASSWORD);
    center.addComponent(username);
    center.addComponent(pass);
    Button signIn = new Button("Sign In");
    signIn.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            if (username.getText().length() == 0 || pass.getText().length() == 0) {
                return;
            }
            UserForm userForm = new UserForm(username.getText(), (EncodedImage) theme.getImage("user.png"), null);
            userForm.show();
        }
    });
    center.addComponent(signIn);
    loginForm.addComponent(BorderLayout.CENTER, center);
    Container bottom = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    Button loginWFace = new Button(theme.getImage("signin_facebook.png"));
    loginWFace.setUIID("LoginButton");
    loginWFace.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            // create your own app identity on facebook follow the guide here:
            // facebook-login.html
            String clientId = "1171134366245722";
            String redirectURI = "http://www.codenameone.com/";
            String clientSecret = "";
            if (clientSecret.length() == 0) {
                System.err.println("create your own facebook app follow the guide here:");
                System.err.println("http://www.codenameone.com/facebook-login.html");
                return;
            }
            Login fb = FacebookConnect.getInstance();
            fb.setClientId(clientId);
            fb.setRedirectURI(redirectURI);
            fb.setClientSecret(clientSecret);
            login = fb;
            fb.setCallback(new LoginListener(LoginListener.FACEBOOK));
            if (!fb.isUserLoggedIn()) {
                fb.doLogin();
            } else {
                showFacebookUser(fb.getAccessToken().getToken());
            }
        }
    });
    Button loginWG = new Button(theme.getImage("signin_google.png"));
    loginWG.setUIID("LoginButton");
    loginWG.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            SignIn.this.doFirebase();
            // create your own google project follow the guide here:
            // http://www.codenameone.com/google-login.html
            String clientId = "555462747934-iujpd5saj4pjpibo7c6r9tbjfef22rh1.apps.googleusercontent.com";
            String redirectURI = "https://www.codenameone.com/oauth2callback";
            String clientSecret = "650YqplrnAI0KXb9LMUnVNnx";
            if (clientSecret.length() == 0) {
                System.err.println("create your own google project follow the guide here:");
                System.err.println("http://www.codenameone.com/google-login.html");
                return;
            }
            Login gc = GoogleConnect.getInstance();
            gc.setClientId(clientId);
            gc.setRedirectURI(redirectURI);
            gc.setClientSecret(clientSecret);
            gc.setScope("https://www.googleapis.com/auth/plus.profile.emails.read https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file");
            login = gc;
            gc.setCallback(new LoginListener(LoginListener.GOOGLE));
            if (!gc.isUserLoggedIn()) {
                gc.doLogin();
            } else {
                showGoogleUser(gc.getAccessToken().getToken());
            }
        }
    });
    bottom.addComponent(loginWFace);
    bottom.addComponent(loginWG);
    loginForm.addComponent(BorderLayout.SOUTH, bottom);
    loginForm.show();
}
Also used : FlowLayout(com.codename1.ui.layouts.FlowLayout) Form(com.codename1.ui.Form) ActionEvent(com.codename1.ui.events.ActionEvent) BoxLayout(com.codename1.ui.layouts.BoxLayout) Label(com.codename1.ui.Label) Login(com.codename1.social.Login) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage) Container(com.codename1.ui.Container) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) Button(com.codename1.ui.Button) TextField(com.codename1.ui.TextField)

Example 8 with Login

use of com.codename1.social.Login in project CodenameOne by codenameone.

the class FaceBookAccess method anonymousLogin.

/**
 * Some simple queries for public data can work just fine with anonymous login without requiring the whole
 * OAuth process, they still need a facebook application though
 * @param appid the id of the application
 * @param clientSecret the client secret for the application
 */
public static void anonymousLogin(String appid, String clientSecret) {
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setUrl("https://graph.facebook.com/oauth/access_token");
    req.addArgument("client_id", appid);
    req.addArgument("client_secret", clientSecret);
    req.addArgument("grant_type", "client_credentials");
    NetworkManager.getInstance().addToQueueAndWait(req);
    if (req.getResponseData() != null) {
        token = new String(req.getResponseData());
        token = token.substring(token.indexOf('=') + 1);
    }
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Example 9 with Login

use of com.codename1.social.Login in project CodenameOne by codenameone.

the class CloudStorage method uploadCloudFileImpl.

private String uploadCloudFileImpl(String mimeType, String file, InputStream data, int dataSize) throws CloudException, IOException {
    String token = CloudPersona.getCurrentPersona().getToken();
    if (token == null || token.length() == 0) {
        if (!CloudPersona.createAnonymous()) {
            throw new CloudException(RETURN_CODE_FAIL_SERVER_ERROR, "Error creating anonymous login");
        }
        token = CloudPersona.getCurrentPersona().getToken();
    }
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setUrl(SERVER_URL + "/fileStoreURLRequest");
    // req.addArgument("bb", Display.getInstance().getProperty("built_by_user", null));
    NetworkManager.getInstance().addToQueueAndWait(req);
    int rc = req.getResponseCode();
    if (rc != 200) {
        if (rc == 420) {
            throw new CloudException(RETURN_CODE_FAIL_QUOTA_EXCEEDED);
        }
        throw new CloudException(RETURN_CODE_FAIL_SERVER_ERROR);
    }
    String d = new String(req.getResponseData());
    MultipartRequest uploadReq = new MultipartRequest();
    uploadReq.setUrl(d);
    uploadReq.setManualRedirect(false);
    uploadReq.addArgument("bb", Display.getInstance().getProperty("built_by_user", null));
    uploadReq.addArgument("t", CloudPersona.getCurrentPersona().getToken());
    uploadReq.addArgument("pk", Display.getInstance().getProperty("package_name", null));
    if (data == null) {
        int pos = file.lastIndexOf('/');
        String shortName = file;
        if (pos > -1) {
            shortName = file.substring(pos);
        }
        uploadReq.addData(shortName, file, mimeType);
    } else {
        uploadReq.addData(file, data, dataSize, mimeType);
    }
    NetworkManager.getInstance().addToQueueAndWait(uploadReq);
    if (uploadReq.getResponseCode() != 200) {
        throw new CloudException(RETURN_CODE_FAIL_SERVER_ERROR);
    }
    String r = new String(uploadReq.getResponseData());
    if ("ERROR".equals(r)) {
        throw new CloudException(RETURN_CODE_FAIL_SERVER_ERROR);
    }
    return r;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) MultipartRequest(com.codename1.io.MultipartRequest)

Example 10 with Login

use of com.codename1.social.Login in project CodenameOne by codenameone.

the class TwitterRESTService method initToken.

/**
 * Logs in to twitter as an application
 *
 * @param consumerKey the key to login with
 * @param consumerSecret the secret to to login with
 * @return the authorization token
 */
public static String initToken(String consumerKey, String consumerSecret) {
    ConnectionRequest auth = new ConnectionRequest() {

        protected void readResponse(InputStream input) throws IOException {
            JSONParser p = new JSONParser();
            Hashtable h = p.parse(new InputStreamReader(input));
            authToken = (String) h.get("access_token");
            if (authToken == null) {
                return;
            }
        }
    };
    auth.setPost(true);
    auth.setUrl("https://api.twitter.com/oauth2/token");
    // YOU MUST CHANGE THIS IF YOU BUILD YOUR OWN APP
    String encoded = Base64.encodeNoNewline((consumerKey + ":" + consumerSecret).getBytes());
    auth.addRequestHeader("Authorization", "Basic " + encoded);
    auth.setContentType("application/x-www-form-urlencoded;charset=UTF-8");
    auth.addArgument("grant_type", "client_credentials");
    NetworkManager.getInstance().addToQueueAndWait(auth);
    return authToken;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Hashtable(java.util.Hashtable) JSONParser(com.codename1.io.JSONParser)

Aggregations

ConnectionRequest (com.codename1.io.ConnectionRequest)6 IOException (java.io.IOException)5 ActionEvent (com.codename1.ui.events.ActionEvent)4 Intent (android.content.Intent)3 IntentResultListener (com.codename1.impl.android.IntentResultListener)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)3 InputStream (java.io.InputStream)3 CodenameOneActivity (com.codename1.impl.android.CodenameOneActivity)2 AccessToken (com.codename1.io.AccessToken)2 JSONParser (com.codename1.io.JSONParser)2 Dialog (com.codename1.ui.Dialog)2 Form (com.codename1.ui.Form)2 ActionListener (com.codename1.ui.events.ActionListener)2 CallbackManager (com.facebook.CallbackManager)2 LoginManager (com.facebook.login.LoginManager)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2 InputStreamReader (java.io.InputStreamReader)2 Hashtable (java.util.Hashtable)2 Map (java.util.Map)2