简体   繁体   中英

Using Twitters API in Android with a Parse.com backend

I am trying to use Parse.com on my Android Application to authenticate my users with Twitter.

Basically, I have a 'Register with Twitter' button. When the user presses this, the Twitter dialog opens, and he signs up. Here is the onClick() function for the Register With Twitter button:

public void registerTwitter(View view)
{
    ParseTwitterUtils.logIn(this, new LogInCallback() {
          @Override
          public void done(ParseUser user, ParseException err) {
            if (user == null) {
                               //do something else, User quit the dialog
            } 
            else if (user.isNew()) 
            {
                Intent i=new Intent(getApplicationContext(),Welcome.class);
                startActivity(i);
            } 
            else 
            {
                //User already exists, do something else
            }
          }
        });
}

Here, the code is working, and there is a ParseUser being created in the back as intended. However, the problem is: how do I get the other public data from the Twitter API to add to my ParseUser object? Like First Name, Last Name, Image, Email etc?

All I get at the backend is a number in the 'Username' field, and the authData object from Twitter

Any help would be appreciated! Thank you

 TwitterInfo twitterInfo = null; Twitter twitter = ParseTwitterUtils.getTwitter(); String twitterUrl = "https://api.twitter.com/1.1/users/show.json?user_id=" + twitter.getUserId(); HttpUriRequest request = new HttpGet(twitterUrl); twitter.signRequest(request); HttpClient client = new DefaultHttpClient(); try { HttpResponse response = client.execute(request); BufferedReader input = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String result = input.readLine(); Log.d(TAG, "getTwitterInfo: result=" + result); JSONObject JsonResponse = new JSONObject(result); String profileImageUrl = JsonResponse.getString("profile_image_url"); String fullName = JsonResponse.getString("name"); twitterInfo = new TwitterInfo(fullName, profileImageUrl); return twitterInfo; } catch(ClientProtocolException e) { throw new CamoPhotoException(e.getMessage()); } catch(IOException e) { throw new CamoPhotoException(e.getMessage()); } catch(JSONException e) { throw new CamoPhotoException(e.getMessage()); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM