Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Plus login not work #5

Closed
stepenik opened this issue Mar 16, 2017 · 16 comments
Closed

Google Plus login not work #5

stepenik opened this issue Mar 16, 2017 · 16 comments

Comments

@stepenik
Copy link

Hi,

When I click on login button on Google plus nothing happen. Do you know what is wrong

public class PageLoginFragment extends Fragment implements OnLoginCompleteListener {

    private View view;

    private EasyLogin mEasyLogin;
    ArrayList<String> fbScope;
    private LoginButton loginButton;
    private SignInButton gPlusButton;
    FacebookNetwork facebook;
    GooglePlusNetwork gPlusNetwork;



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mEasyLogin.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public void onLoginSuccess(SocialNetwork.Network network) {
        if (network == SocialNetwork.Network.FACEBOOK) {
            AccessToken token = mEasyLogin.getSocialNetwork(SocialNetwork.Network.FACEBOOK).getAccessToken();
            String fbtok = token.getToken();
            System.out.println(fbtok);
        }else if (network == SocialNetwork.Network.GOOGLE_PLUS) {
            AccessToken token = mEasyLogin.getSocialNetwork(SocialNetwork.Network.GOOGLE_PLUS).getAccessToken();
            String gptok = token.getToken();
            System.out.println(gptok);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        EasyLogin.initialize();
        mEasyLogin = EasyLogin.getInstance();

        view = inflater.inflate(R.layout.page_login_fragment, container, false);

        // FACEBOOK
        fbScope = new ArrayList<>();
        fbScope.addAll(Collections.singletonList("public_profile, email"));
        mEasyLogin.addSocialNetwork(new FacebookNetwork((MainActivity)getActivity(), fbScope));

        facebook = (FacebookNetwork) mEasyLogin.getSocialNetwork(SocialNetwork.Network.FACEBOOK);
        facebook.setOnLoginCompleteListener(this);
        loginButton = (LoginButton) view.findViewById(R.id.login_button);
        // Call this method if you are using the LoginButton provided by facebook
        // It can handle its own state
        if (!facebook.isConnected()) {
            facebook.requestLogin(loginButton,this);
        }

        mEasyLogin.addSocialNetwork(new GooglePlusNetwork((MainActivity)getActivity()));
        gPlusNetwork = (GooglePlusNetwork) mEasyLogin.getSocialNetwork(SocialNetwork.Network.GOOGLE_PLUS);
        gPlusNetwork.setOnLoginCompleteListener(this);

        gPlusButton = (SignInButton) view.findViewById(R.id.gplus_sign_in_button);
        gPlusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!gPlusNetwork.isConnected()) {
                    gPlusNetwork.requestLogin(PageLoginFragment.this);
                }
            }
        });



        updateStatuses();

        return view;
    }

    private void updateStatuses() {
        StringBuilder content = new StringBuilder();
        for (SocialNetwork socialNetwork : mEasyLogin.getInitializedSocialNetworks()) {
            content.append(socialNetwork.getNetwork())
                    .append(": ")
                    .append(socialNetwork.isConnected())
                    .append("\n");
        }
        System.out.println(content.toString());
    }

    @Override
    public void onError(SocialNetwork.Network socialNetwork, String requestID, String errorMessage) {
        Log.e("MAIN", "ERROR!" + socialNetwork + "|||" + errorMessage);

    }
}
@stepenik
Copy link
Author

stepenik commented Mar 16, 2017

gPlusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!gPlusNetwork.isConnected()) {
                    gPlusNetwork.requestLogin(PageLoginFragmentNew.this);
                }
                boolean testcon = gPlusNetwork.isConnected();
            }
        });

Problem is when I click on button gPlusNetwork.isConnected() is true but I am not login with ! if is false.
I am trying to remove ! like that

if (gPlusNetwork.isConnected()) {
   gPlusNetwork.requestLogin(PageLoginFragmentNew.this);
}

But now I have java.lang.RuntimeException: Already connected, please check isConnected() method. But I am not logged. I am not seeing popup window for login when I click on button

@stepenik
Copy link
Author

@maksim88 This also not working in MainActivity... Any news for this bug?

@maksim88
Copy link
Owner

Hey,

i'm currently investigating...
GoogleApiClient behaves very weirdly I have to say.
isConnected() cannot be checked at first, but returns true after onStart().
Also there is no easy way to refetch the AccessToken when you are already connected (based on GoogleApiClient)
I'm investigating whether I can silently login and refetch the token that way.
I'll keep you uptodate when I have something new.
For now you can always logout(), and requestLogin() afterwards.
What's your use case exactly?

@stepenik
Copy link
Author

stepenik commented Mar 16, 2017

In MainActivity I am put 2 button one for Fb and one for G+. When I click on fb button everything works fine. But when I click on G+ button nothing happen. I am run this on Emulator (Android 5, Android 6, Android 7) and on my real device which is Android 5. In all cases same story nothing happen. This is like a button with no events.

@maksim88
Copy link
Owner

Ok, so the button does not do anything because you are checking for the connection state (isConnected()) and GoogleApiClient returns true in that moment.

I'll upload a new version to jitpack where you can test whether it works better for you. I made some changes to GooglePlusNetwork
gPlusButton in the Activity / Fragment now looks like that:


gPlusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!gPlusNetwork.isConnected()) {
                    gPlusNetwork.requestLogin(MainActivity.this);
                } else {
                    gPlusNetwork.setLocalOnCompleteListener(MainActivity.this);
                    gPlusNetwork.silentSignIn();
                }
            }
        });

in the connected state you can call silentSignIn() and get a token back.
It will be v0.3.1 on jitpack

@stepenik
Copy link
Author

Yes, now this works... But when you logged you see button Sign In maybe this replace with button text LogOut...

@stepenik
Copy link
Author

stepenik commented Mar 16, 2017

When you login with Fb and G+ in same time on Fb button you see LogOut but G+ button stay same text (Sign In). Now you go turn off your device and now you go turn on your device. On facebook you stay logged but on G+ you are not logged.

@maksim88
Copy link
Owner

Unfortunately the Google SignInButton does not handle its own state like the Facebook button does, that's why it is not possible.

What I saw people do and what I actually liked when I looked at some projects is to call silentSignIn() in onStart() of your Activity/ Fragment and if it succeeded then call gPlusButton.setEnabled(false).
Then you will need another mechanism to disconnect (but you need it in all the cases with Google Plus as there is no way to reuse the SignInButton)

@stepenik
Copy link
Author

What you think about that idea.

For example you go login with G+ and now in method onLoginSuccess() you have token, username, email save that in SharedPreferences and now you go turn off your device. Now you turn on your device in fragment in method onCreateView you invoke SharedPreferences data. If you have data you are logged. But now is question if you want to referesh your username or email token is outdatet. Do you have any method to give you new token if you not click on button.

@stepenik
Copy link
Author

For example....

For facebook this work if you not logout and turn off/on your device
facebook.isConnected() = true

if(facebook.isConnected()) {
            AccessToken token = mEasyLogin.getSocialNetwork(SocialNetwork.Network.FACEBOOK).getAccessToken();
            String fbtok = token.getToken();
            System.out.println(fbtok);
        }

For google
gPlusNetwork.isConnected() = false
after device turn on/off

if(gPlusNetwork.isConnected()){
            AccessToken token = mEasyLogin.getSocialNetwork(SocialNetwork.Network.GOOGLE_PLUS).getAccessToken();
            String gptok = token.getToken();
            System.out.println(gptok);
        }

@maksim88
Copy link
Owner

I'll play around with it on the weekend and think about it

@stepenik
Copy link
Author

Ok nice. When you will have new informations share it here 👍

@stepenik
Copy link
Author

Do you have any news :)

@maksim88
Copy link
Owner

I decided just to save the connection state in the SharedPreferences (because I do not like saving credentials in SP). Just check the sample project to see how I envision it to work.

@stepenik
Copy link
Author

I will test now

@stepenik
Copy link
Author

@maksim88 Perfect this works fantastic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants