W = F*X

  • Örnek sayfa

Android Linkendin Login Örneği

05. Haziran 2013 · Write a comment · Categories: android · Tags: android linkedin login, android linkedin login example, linkedin login example

Linkedin ile login olabilmeniz için öncelikle https://www.linkedin.com/secure/developer adresinden kayıt olup yeni bir uygulama oluşturmanız gerekmektedir.
Projenin libs klasöründe şağıdaki jarlar bulunmaktadır.

  • commons-codec-1.3.jar
  • linkedin-j-android.jar
  • signpost-core-1.2.1.1.jar
  • Bu işlemleri yaptıktan sonra
    Şöyle bir main.xml layoutunuz olmalıdır.

    main.xml

     
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/tv"
        />
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
    
    </LinearLayout>
    
    

    Manifest.xmlde şöyle birşey olabilir.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="pl.selvin.android.LinkedInTest"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk android:minSdkVersion="4" />
    
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application android:label="LinkedInTest" >
            <activity
                android:name=".LITestActivity"
                android:label="LinkedIn Test"
                android:launchMode="singleInstance" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
    
                    <data
                        android:host="litestcalback"
                        android:scheme="x-oauthflow-linkedin" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

     
    Son olarakta LITestActivity sinifinimizda aşağıdaki gibi olmalıdır.

    package pl.selvin.android.LinkedInTest;
    
    import java.util.Arrays;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.google.code.linkedinapi.client.LinkedInApiClient;
    import com.google.code.linkedinapi.client.LinkedInApiClientException;
    import com.google.code.linkedinapi.client.LinkedInApiClientFactory;
    import com.google.code.linkedinapi.client.oauth.LinkedInAccessToken;
    import com.google.code.linkedinapi.client.oauth.LinkedInOAuthService;
    import com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceFactory;
    import com.google.code.linkedinapi.client.oauth.LinkedInRequestToken;
    import com.google.code.linkedinapi.schema.Person;
    
    public class LITestActivity extends Activity {
    
    	static final String CONSUMER_KEY = "api keyiniz";
    	static final String CONSUMER_SECRET = "secret keyiniz";
    
    	static final String APP_NAME = "LITest";
    	static final String OAUTH_CALLBACK_SCHEME = "x-oauthflow-linkedin";
    	static final String OAUTH_CALLBACK_HOST = "litestcalback";
    	static final String OAUTH_CALLBACK_URL = String.format("%s://%s",
    			OAUTH_CALLBACK_SCHEME, OAUTH_CALLBACK_HOST);
    	static final String OAUTH_QUERY_TOKEN = "oauth_token";
    	static final String OAUTH_QUERY_VERIFIER = "oauth_verifier";
    	static final String OAUTH_QUERY_PROBLEM = "oauth_problem";
    
    	final LinkedInOAuthService oAuthService = LinkedInOAuthServiceFactory
    			.getInstance().createLinkedInOAuthService(CONSUMER_KEY,
    					CONSUMER_SECRET);
    	final LinkedInApiClientFactory factory = LinkedInApiClientFactory
    			.newInstance(CONSUMER_KEY, CONSUMER_SECRET);
    
    	static final String OAUTH_PREF = "LIKEDIN_OAUTH";
    	static final String PREF_TOKEN = "token";
    	static final String PREF_TOKENSECRET = "tokenSecret";
    	static final String PREF_REQTOKENSECRET = "requestTokenSecret";
    
    	TextView tv = null;
    	LinkedInApiClient client;
    
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		tv = (TextView) findViewById(R.id.tv);
    
    		final SharedPreferences pref = getSharedPreferences(OAUTH_PREF,
    				MODE_PRIVATE);
    		final String token = pref.getString(PREF_TOKEN, null);
    		final String tokenSecret = pref.getString(PREF_TOKENSECRET, null);
    		if (token == null || tokenSecret == null) {
    			startAutheniticate();
    		} else {
    			showCurrentUser(new LinkedInAccessToken(token, tokenSecret));
    		}
    
    		
    
    	}
    
    	void startAutheniticate() {
    		final LinkedInRequestToken liToken = oAuthService
    				.getOAuthRequestToken(OAUTH_CALLBACK_URL);
    		final String uri = liToken.getAuthorizationUrl();
    		getSharedPreferences(OAUTH_PREF, MODE_PRIVATE).edit()
    				.putString(PREF_REQTOKENSECRET, liToken.getTokenSecret())
    				.commit();
    		Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    		startActivity(i);
    	}
    
    	void finishAuthenticate(final Uri uri) {
    		if (uri != null && uri.getScheme().equals(OAUTH_CALLBACK_SCHEME)) {
    			final String problem = uri.getQueryParameter(OAUTH_QUERY_PROBLEM);
    			if (problem == null) {
    				final SharedPreferences pref = getSharedPreferences(OAUTH_PREF,
    						MODE_PRIVATE);
    				final LinkedInAccessToken accessToken = oAuthService
    						.getOAuthAccessToken(
    								new LinkedInRequestToken(uri
    										.getQueryParameter(OAUTH_QUERY_TOKEN),
    										pref.getString(PREF_REQTOKENSECRET,
    												null)),
    								uri.getQueryParameter(OAUTH_QUERY_VERIFIER));
    				pref.edit()
    						.putString(PREF_TOKEN, accessToken.getToken())
    						.putString(PREF_TOKENSECRET,
    								accessToken.getTokenSecret())
    						.remove(PREF_REQTOKENSECRET).commit();
    				showCurrentUser(accessToken);
    			} else {
    				Toast.makeText(this,
    						"Appliaction down due OAuth problem: " + problem,
    						Toast.LENGTH_LONG).show();
    				finish();
    			}
    
    		}
    	}
    
    	void clearTokens() {
    		getSharedPreferences(OAUTH_PREF, MODE_PRIVATE).edit()
    				.remove(PREF_TOKEN).remove(PREF_TOKENSECRET)
    				.remove(PREF_REQTOKENSECRET).commit();
    	}
    
    	void showCurrentUser(final LinkedInAccessToken accessToken) {
    		client = factory.createLinkedInApiClient(accessToken);
    
    		try {
    			final Person p = client.getProfileForCurrentUser();
    			// /////////////////////////////////////////////////////////
    			// here you can do client API calls ...
    			// client.postComment(arg0, arg1);
    			// client.updateCurrentStatus(arg0);
    			// or any other API call (this sample only check for current user
    			// and shows it in TextView)
    			// /////////////////////////////////////////////////////////
    			tv.setText(p.getLastName() + ", " + p.getFirstName());
    		} catch (LinkedInApiClientException ex) {
    			clearTokens();
    			Toast.makeText(
    					this,
    					"Appliaction down due LinkedInApiClientException: "
    							+ ex.getMessage()
    							+ " Authokens cleared - try run application again.",
    					Toast.LENGTH_LONG).show();
    			finish();
    		}
    
    	}
    
    	@Override
    	protected void onNewIntent(Intent intent) {
    		finishAuthenticate(intent.getData());
    	}
    }
    

    Son Yazılar

    • SQL Injection
    • Hackathonist
    • java.IO.EOFException tomcat
    • Android SharedPreferences
    • Intent veri geçirme

    Son Yorumlar

    • Laptop access point için Online casino
    • Intent veri geçirme için Nedim
    • android.os.NetworkOnMainThreadException için Geralynn
    • Html z-index için Cassie
    • Html z-index için Lynda

    Arşivler

    Kategoriler

    • ağ
    • android
    • eclipse
    • Genel
    • glassfish
    • güvenlik
    • gwt
    • hibernate
    • Java
    • Linux
    • mac
    • tomcat
    • veri tabani

    Meta

    • Giriş
    • Yazılar RSS
    • Yorumlar RSS
    • WordPress.org
    © 2019 W = F*X. All rights reserved.
    Design by picomol. Powered by WordPress.