【问题标题】:How to get LinkedIn Profile information other then the default information Android如何获取除默认信息 Android 之外的 LinkedIn 个人资料信息
【发布时间】:2012-04-20 20:49:30
【问题描述】:

我正在获取用户的默认信息,例如名字和姓氏一个 URL(站点标准 URL),但我想访问 User_id 和 smilar 信息。我遵循“https://developer.linkedin.com/documents/profile-api”并像下面的代码一样实现。 请帮帮我。我尝试了很多东西,但都没有成功。

public class LinkedInTestActivity extends Activity {

    public static final String CONSUMER_KEY = "XXXXXXXXXXXXXXXX";
    public static final String CONSUMER_SECRET = "XXXXXXXXXXXXXXXX";`enter code here`
    LinkedInAccessToken accessToken;
    public static final String APP_NAME = "LITest";
    public static final String OAUTH_CALLBACK_SCHEME = "x-oauthflow-linkedin";
    public static final String OAUTH_CALLBACK_HOST = "callback";
    public static final String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME+ "://" + OAUTH_CALLBACK_HOST;

    final LinkedInOAuthService oAuthService = LinkedInOAuthServiceFactory.getInstance().createLinkedInOAuthService(CONSUMER_KEY,CONSUMER_SECRET);
    final LinkedInApiClientFactory factory = LinkedInApiClientFactory.newInstance(CONSUMER_KEY, CONSUMER_SECRET);
    LinkedInRequestToken liToken;
    LinkedInApiClient client;

    TextView tv = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.tv);
        liToken = oAuthService.getOAuthRequestToken(OAUTH_CALLBACK_URL);
        Log.e("liToken.getAuthorizationUrl()",""+liToken.getAuthorizationUrl());
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(liToken.getAuthorizationUrl()));
        startActivity(i);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        String verifier = intent.getData().getQueryParameter("oauth_verifier");
        Log.e("Verifier-------->",""+verifier);
        Log.e("liToken-------->",""+liToken);
        accessToken = oAuthService.getOAuthAccessToken(liToken, verifier);
        Log.e("accessToken-------->",""+accessToken.getToken());
        client = factory.createLinkedInApiClient(accessToken);
        client.postNetworkUpdate("LinkedIn Android app test");
        //      Person p = client.getProfileByUrl("http://api.linkedin.com/v1/people/~", ProfileType.STANDARD);

        Person p =client.getProfileForCurrentUser();
        p.getDateOfBirth();
        p.getApiStandardProfileRequest();
        p.getSiteStandardProfileRequest().getUrl();
        p.getHeadline();
        tv.setText(p.getLastName() + ", " + p.getFirstName()+ " DOB "+ p.getDateOfBirth());

        if (accessToken == null) {
            throw new IllegalArgumentException("access token cannot be null.");
        }
        try {
            URL url = new URL(LinkedInApiUrls.GET_CONNECTIONS_FOR_CURRENT_USER);
            HttpURLConnection request = (HttpURLConnection) url.openConnection();

            final OAuthConsumer consumer = getOAuthConsumer();
            consumer.setTokenWithSecret(accessToken.getToken(), accessToken.getTokenSecret());
            consumer.sign(request);
            request.connect();

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://api.linkedin.com/v1/people/~:(id,first-name,last-name,industry,headline,public-profile-url)");
            consumer.sign(post); // here need the consumer for sign in for post the share
            org.apache.http.HttpResponse response = httpclient.execute(post);
            Log.e("response",""+response.getHeaders(verifier).toString());

    } 

    protected OAuthConsumer getOAuthConsumer() {
        DefaultOAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER
_KEY, CONSUMER_SECRET);
consumer.setMessageSigner(new HmacSha1MessageSigner());
consumer.setSigningStrategy(new AuthorizationHeaderSigningStrategy());
return consumer;
    }
}

【问题讨论】:

  • 这个问题有这么多代码。您应该只有与您的问题相关的部分。不要放太多,以免激怒读者。阅读常见问题解答

标签: android integration linkedin access-token


【解决方案1】:

如需获取更多用户详细信息,应使用 Person 对象,如下所示,

请更换这个,

Person p =client.getProfileForCurrentUser();

与,

Person profile = client.getProfileForCurrentUser(EnumSet.of(
                ProfileField.ID, ProfileField.FIRST_NAME,
                ProfileField.LAST_NAME, ProfileField.HEADLINE,
                ProfileField.INDUSTRY, ProfileField.PICTURE_URL,
                ProfileField.DATE_OF_BIRTH, ProfileField.LOCATION_NAME,
                ProfileField.MAIN_ADDRESS, ProfileField.LOCATION_COUNTRY));

因此您可以获得更详细的信息。使用此配置文件对象,

        System.out.println("PersonID : " + profile.getId());
        System.out.println("Name : " + profile.getFirstName() + " "
                + profile.getLastName());
        System.out.println("Headline : " + profile.getHeadline());
        System.out.println("Industry : " + profile.getIndustry());
        System.out.println("Picture : " + profile.getPictureUrl());
        DateOfBirth dateOfBirth = profile.getDateOfBirth();
        System.out.println("DateOfBirth : " + dateOfBirth.getDay() + "/"
                + dateOfBirth.getMonth() + "/" + dateOfBirth.getYear());
        System.out.println("MAin Address : " + profile.getMainAddress());
        Location location = profile.getLocation();
        System.out.println("Location:" + location.getName() + " - "
                + location.getCountry().getCode());

【讨论】:

    【解决方案2】:

    我使用这个方法点击了 url,它对我有用:-

    private OAuthConsumer getConsumer() {
            OAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            consumer.setTokenWithSecret(accessToken.getToken(), accessToken.getTokenSecret());
            return consumer;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多