Get a User's email address from Twitter (Android)

前端 未结 5 1351
悲&欢浪女
悲&欢浪女 2021-01-21 06:49

How can i get email address of a user via twitter API? I\'m using Twitter4j for Sign in with twitter

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-21 07:13

    To Get Twitter Email and Profile Image URL , username, id you to call requestEmail method.

    Also You to allow request email in your developer account of twitter. See below images.

    Use Below code to Get user details.

        btnTwitterLogin.setCallback(new Callback() {
            @Override
            public void success(Result result) {
                TwitterSession session = TwitterCore.getInstance().getSessionManager().getActiveSession();
                TwitterAuthToken authToken = session.getAuthToken();
                TwitterAuthClient authClient = new TwitterAuthClient();
                authClient.requestEmail(session, new Callback() {
                    @Override
                    public void success(Result result) {
                        String email =result.data;
                        String token = authToken.token;
                        String secret = authToken.secret;
                        String username = session.getUserName();
                        long userId = session.getUserId();
                        Log.e("neem", "success: \n Token =>  " + token + "\n Secret: " + secret + " \n Username: " + username + "\n Userid: " + userId+" \n Email: "+email);
    
                    }
    
                    @Override
                    public void failure(TwitterException exception) {
                        Log.e("neem", "failure: "+exception.getMessage() );
                    }
                });
                TwitterCore.getInstance().getApiClient(result.data).getAccountService().verifyCredentials(false,true,false).enqueue(new Callback() {
                    @Override
                    public void success(Result userResult) {
                        try {
    
                            String imageUrl = userResult.data.profileImageUrl;
                            String username = userResult.data.screenName;
                            Log.e("neem", "acount services: imageurl: "+imageUrl+"\n username: "+username );
    
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void failure(TwitterException e) {
                    }
                });
    
            }
    
            @Override
            public void failure(TwitterException exception) {
                ShowToast(mcontext, "Login failed due to: \n "+exception.getMessage());
            }
        });
    

提交回复
热议问题