Twitter Share in Android
--------------------------------------------
--------------------------------------------
In this we use the following constants--
public static final String CONSUMER_KEY = "Your Consumer key";
public static final String CONSUMER_SECRET= "Your Consumer Secret Key";
public static final String REQUEST_URL = "http://api.twitter.com/oauth/request_token";
public static final String ACCESS_URL = "http://api.twitter.com/oauth/access_token";
public static final String AUTHORIZE_URL = "https://api.twitter.com/oauth/authenticate";
public static final String OAUTH_CALLBACK_URL= "call back url";
OAUTH_CALLBACK_URL is used for return back to the application after getting oauth token.This call back url is declared in
manifest file(Corresponding oauth token generating activity)
OAUTH_CALLBACK_URL is defined at the time of app creation in twitter.
In this,we use webview client for loading the url
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.authorization_view);
setUpViews();
String authURL = beginAuthorization();
webView.loadUrl(authURL);
}
private void setUpViews() {
webView = (WebView) findViewById(R.id.web_view);
webView.setWebViewClient(webViewClient);
}
public String beginAuthorization() {
try {
final OAuthHmacSigner signer = new OAuthHmacSigner();
OAuthAuthorizeTemporaryTokenUrl authorizeUrl;
signer.clientSharedSecret = Constants.CONSUMER_SECRET;
OAuthGetTemporaryToken temporaryToken = new OAuthGetTemporaryToken(Constants.REQUEST_URL);
temporaryToken.transport = new ApacheHttpTransport();
temporaryToken.signer = signer;
temporaryToken.consumerKey = Constants.CONSUMER_KEY;
temporaryToken.callback = Constants.OAUTH_CALLBACK_URL;
OAuthCredentialsResponse tempCredentials = temporaryToken.execute();
signer.tokenSharedSecret = tempCredentials.tokenSecret;
authorizeUrl = new OAuthAuthorizeTemporaryTokenUrl(Constants.AUTHORIZE_URL);
authorizeUrl.temporaryToken = tempCredentials.token;
String authorizationUrl = authorizeUrl.build();
System.out.println(authorizationUrl);
return authorizationUrl+"&force_login=true"; // "&force_login=true" is used for asking asking login every time
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private WebViewClient webViewClient = new WebViewClient() {
@Override
public void onLoadResource(WebView view, String url) {
final OAuthHmacSigner signer = new OAuthHmacSigner();
Uri uri = Uri.parse(url);
if (url.startsWith(Constants.OAUTH_CALLBACK_URL)) {
try {
if (url.indexOf("oauth_token=")!=-1) {
token = uri.getQueryParameter("oauth_token");
verifier = uri.getQueryParameter("oauth_verifier");
signer.clientSharedSecret = Constants.CONSUMER_SECRET;
OAuthGetAccessToken accessToken = new OAuthGetAccessToken(
Constants.ACCESS_URL);
accessToken.transport = new ApacheHttpTransport();
accessToken.temporaryToken = token;
accessToken.signer = signer;
accessToken.consumerKey = Constants.CONSUMER_KEY;
accessToken.verifier = verifier;
credentials = accessToken.execute();
signer.tokenSharedSecret = credentials.tokenSecret;
System.out.println("token" + credentials.token + "Token Secret"
finish();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
--------------------------------------------
--------------------------------------------
In this we use the following constants--
public static final String CONSUMER_KEY = "Your Consumer key";
public static final String CONSUMER_SECRET= "Your Consumer Secret Key";
public static final String REQUEST_URL = "http://api.twitter.com/oauth/request_token";
public static final String ACCESS_URL = "http://api.twitter.com/oauth/access_token";
public static final String AUTHORIZE_URL = "https://api.twitter.com/oauth/authenticate";
public static final String OAUTH_CALLBACK_URL= "call back url";
OAUTH_CALLBACK_URL is used for return back to the application after getting oauth token.This call back url is declared in
manifest file(Corresponding oauth token generating activity)
OAUTH_CALLBACK_URL is defined at the time of app creation in twitter.
In this,we use webview client for loading the url
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.authorization_view);
setUpViews();
String authURL = beginAuthorization();
webView.loadUrl(authURL);
}
private void setUpViews() {
webView = (WebView) findViewById(R.id.web_view);
webView.setWebViewClient(webViewClient);
}
public String beginAuthorization() {
try {
final OAuthHmacSigner signer = new OAuthHmacSigner();
OAuthAuthorizeTemporaryTokenUrl authorizeUrl;
signer.clientSharedSecret = Constants.CONSUMER_SECRET;
OAuthGetTemporaryToken temporaryToken = new OAuthGetTemporaryToken(Constants.REQUEST_URL);
temporaryToken.transport = new ApacheHttpTransport();
temporaryToken.signer = signer;
temporaryToken.consumerKey = Constants.CONSUMER_KEY;
temporaryToken.callback = Constants.OAUTH_CALLBACK_URL;
OAuthCredentialsResponse tempCredentials = temporaryToken.execute();
signer.tokenSharedSecret = tempCredentials.tokenSecret;
authorizeUrl = new OAuthAuthorizeTemporaryTokenUrl(Constants.AUTHORIZE_URL);
authorizeUrl.temporaryToken = tempCredentials.token;
String authorizationUrl = authorizeUrl.build();
System.out.println(authorizationUrl);
return authorizationUrl+"&force_login=true"; // "&force_login=true" is used for asking asking login every time
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private WebViewClient webViewClient = new WebViewClient() {
@Override
public void onLoadResource(WebView view, String url) {
final OAuthHmacSigner signer = new OAuthHmacSigner();
Uri uri = Uri.parse(url);
if (url.startsWith(Constants.OAUTH_CALLBACK_URL)) {
try {
if (url.indexOf("oauth_token=")!=-1) {
token = uri.getQueryParameter("oauth_token");
verifier = uri.getQueryParameter("oauth_verifier");
signer.clientSharedSecret = Constants.CONSUMER_SECRET;
OAuthGetAccessToken accessToken = new OAuthGetAccessToken(
Constants.ACCESS_URL);
accessToken.transport = new ApacheHttpTransport();
accessToken.temporaryToken = token;
accessToken.signer = signer;
accessToken.consumerKey = Constants.CONSUMER_KEY;
accessToken.verifier = verifier;
credentials = accessToken.execute();
signer.tokenSharedSecret = credentials.tokenSecret;
System.out.println("token" + credentials.token + "Token Secret"
finish();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
0 comments: