I want to make an android app which will update status in twitter.
I am using signpost-core-1.2.1.1 and signpost-commonshttp4-1.2.1.1 jar files.I have given internet uses permission and i have registered app in twitter giving read,write and direct messages permission. Also filled up the callback Url.
code snippet:
private static final String CALLBACK_URI = "app://twitter";
private static final String REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token";
private static final String ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token";
private static final String AUTHORIZE_URL = "https://api.twitter.com/oauth/authorize";
String CONSUMER_KEY = "XXXXXXXXXX";
String CONSUMER_SECRET = "XXXXXXXXXX";
private static CommonsHttpOAuthConsumer consumer;
private static CommonsHttpOAuthProvider provider;
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
provider = new DefaultOAuthProvider(REQUEST_TOKEN_URL,ACCESS_TOKEN_URL, AUTHORIZE_URL);
String authUrl="";
authUrl = provider.retrieveRequestToken(consumer,CALLBACK_URI);
I am totally stuck with this.Please reply.
Signpost documentation says DefaultOAuthProvider has some problem with Android implementation of java.net. They suggest use of CommonsHttpOAuthProvider instead. I was having the same problem. This solved this exception in my case. Hope this helps.
First By android application we are not allowed to send callbackurl, so use "oob" instead of callbackurl, and there is an api for twitter which is also supported on android, it is jTwitter.
Related
I am in a Android project of creating a Google Reader client.
I with to use built in account credential and to be as close of the official google reader app on that point.
I'm struggling in a problem since a week and can't find out how to solve it.
I managed to get an Auth Token from AccountManager using authtokentype "reader".
The token is of the form
DQAAAOcAAACJAmAkHEpPnaP-v7HxPYyz3XtCNwTiFLUsqYXfLQvCko4AqUyY213U81fXeNZC3VVArBaEsJJFcRXDmczrEhDquxiJiWSp7eM9T8Bs2VV3uqFwE7irrfarKyBH-oVn2nZxKc3wcugqa7k3p4KCotz48cQRslDQhSZj1AK4hxqO2eu5X8lutffRCPjA6kmgmXhkYA4RuTCxVXbZr0E_ytQteX2VzOlPbqrZ7QD7NiqbeJYODE3pk4BNjHTWtgXAr3J0U7LMdtRh9aC9AJoBp1SNl3jptyzqFfeEjWtVTBQlKtn40vyzMDt0sZoSIaSPquc
Now that I have the token, how do I login with the unofficial Google Reader api ?
I know this java api, and found various documentation here, here and here. All of these only lay the emphasis upon Client Login with email and password. Each time I find a code sample it is of the form
token = someFunctionIAlreadyUse();
someUndocumentedUnknowPersonalFunction(token);
Am I missing a simple way ? If not where can I find a detailled way of accessing the api (in java) ?
Just finding out how to do. The token given by android is the same as the token used in the regular google reader authentication. Here is some code from Christian Dadswell using Jsoup and modified to work with the Android authtoken :
public static String getGoogleToken(String authKey) throws UnsupportedEncodingException, IOException
{
Document doc = Jsoup.connect(_TOKEN_URL).header("Authorization", _AUTHPARAMS + authKey).userAgent(_USER_AGENT).timeout(4000).get();
// RETRIEVES THE RESPONSE TOKEN
String _TOKEN = doc.body().text();
return _TOKEN;
}
and
public static String getUserInfo(String authKey) throws UnsupportedEncodingException, IOException
{
Document doc = Jsoup.connect(_USER_INFO_URL).header("Authorization", _AUTHPARAMS + authKey).userAgent(_USER_AGENT).timeout(4000).get();
// RETRIEVES THE RESPONSE USERINFO
String _USERINFO = doc.body().text();
return _USERINFO;
}
And constants :
private static String _USER_AGENT = "YourAppNameHere";
private static final String _AUTHPARAMS = "GoogleLogin auth=";
private static final String _GOOGLE_LOGIN_URL = "https://www.google.com/accounts/ClientLogin";
private static final String _READER_BASE_URL = "http://www.google.com/reader/";
private static final String _API_URL = _READER_BASE_URL + "api/0/";
private static final String _TOKEN_URL = _API_URL + "token";
private static final String _USER_INFO_URL = _API_URL + "user-info";
private static final String _USER_LABEL = "user/-/label/";
private static final String _TAG_LIST_URL = _API_URL + "tag/list";
private static final String _EDIT_TAG_URL = _API_URL + "tag/edit";
private static final String _RENAME_TAG_URL = _API_URL + "rename-tag";
private static final String _DISABLE_TAG_URL = _API_URL + "disable-tag";
private static final String _SUBSCRIPTION_URL = _API_URL + "subscription/edit";
private static final String _SUBSCRIPTION_LIST_URL = _API_URL + "subscription/list";
I trying to register c2dm, but allways give me this error:
05-18 10:48:32.357: D/C2DMRegistrar(225): [C2DMRegistrar.46] register: http error 400
this is the register method:
public static final String EXTRA_SENDER = "sender";
public static final String EXTRA_APPLICATION_PENDING_INTENT = "app";
public static final String REQUEST_UNREGISTRATION_INTENT = "com.google.android.c2dm.intent.UNREGISTER";
public static final String REQUEST_REGISTRATION_INTENT = "com.google.android.c2dm.intent.REGISTER";
public static final String LAST_REGISTRATION_CHANGE = "last_registration_change";
public static final String BACKOFF = "backoff";
// public static final String GSF_PACKAGE = "com.google.android.gsf";
public static void register(Context context, String senderId) {
Intent registrationIntent = new Intent(REQUEST_REGISTRATION_INTENT);
// registrationIntent.setPackage(GSF_PACKAGE);
registrationIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT,
PendingIntent.getBroadcast(context, 0, new Intent(), 0));
registrationIntent.putExtra(EXTRA_SENDER, senderId);
context.startService(registrationIntent);
}
I test it with GSF_PACKAGE, and without GSF_PACKAGE.
SenderId is a mail acount XXXX#XXX.com, and I received a mail like this:
From: noreply#google.com [mailto:noreply#google.com]
Thank you for your interest in Android Cloud to Device Messaging (C2DM).
We've accepted your application into the trial group. The Google account
you requested as the sender account for your application:
XXXX#XXX.com
I´m in AVD emulator, and i have a google account in emulator too. And in real mobile, same thing.
I don´t receive any call in C2DMBaseReceiver.onHandleIntent, only show that error.
Any idea?
Have you set Your Google account through your Emulator, if not do so by Settings-> Account& Sync-> Add an account
Hope this helps !!!
I want to make an android app which will update status in twitter.
I am using signpost-core-1.2.1.1 and signpost-commonshttp4-1.2.1.1 jar files.I have given internet uses permission and i have registered app in twitter giving read,write and direct messages permission. Also filled up the callback Url.But retrieveRequestToken() function throws OAuthCommunicationException.
code snippet:
private static final String CALLBACK_URI = "app://twitter";
private static final String REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token";
private static final String ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token";
private static final String AUTHORIZE_URL = "https://api.twitter.com/oauth/authorize";
String CONSUMER_KEY = "I4bwezijxf6VwpU8x0tygg";
String CONSUMER_SECRET = "Y6vSdZs3zWBrNogXZWSHKZ590RSXqB5wBwj8vFaayk";
private static CommonsHttpOAuthConsumer consumer;
private static CommonsHttpOAuthProvider provider;
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
provider = new DefaultOAuthProvider(REQUEST_TOKEN_URL,ACCESS_TOKEN_URL, AUTHORIZE_URL);
String authUrl="";
authUrl = provider.retrieveRequestToken(consumer,CALLBACK_URI);
I am totally stuck with this.Please reply.
Your consumer credentials are working fine in my test twitter app, so could not find the issue with you.
Here is the complete detailed article on Twitter integration and you can literally copy the code of TwitterConnectWebActivity in your app and call it by an intent for twitter integration.Twitter integration in Android app complete codeThis must solve your problem.
I have some question to ask.
When I use ojdbc14.jar link to oracle 10g in android 3.x sys, that can not connect to oracle.
But I use ojdbc14.jar in android 2.x or 4.0 sys, it can be work.
In 3.x will throw exception is:
java.lang.StringIndexOutOfBoundsException: start=0 end=134
data.length=168 index=168 length=134
Here is part of code:
private static final String DEFAULT_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DEFAULT_URL = "jdbc:oracle:thin:#192.168.0.1:1521:orcl";
private static final String DEFAULT_USERNAME = "admin";
private static final String DEFAULT_PASSWORD = "admin";
try {
Class sClass = java.lang.Class.forName(driverClass);
conn = DriverManager.getConnection(DEFAULT_URL, DEFAULT_USERNAME, DEFAULT_PASSWORD);
setState(conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY));
} catch (Exception e) {
e.printStackTrace();
}
Here is my code, i keep getting an exception "Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match." on this line 'provider.retrieveAccessToken(consumer, verifier);'. I have triple checked my consumer key and secret and my twitter application is set as a Browser and tried setting provider.setOAuth10a(true), i have been struggling on this for 2 days!! I am using signpost 1.2.1.1 (core & commonshttp4), If anyone can help! Please im desperate
private static final String CONSUMER_KEY = "MY_CONSUMER_KEY";
private static final String CONSUMER_SECRET = "MY_CONSUMER_SECRET";
private static final String CALLBACK_URL = "tweet-mapper://mainactivity";
private static final String REQUEST_URL = "https://api.twitter.com/oauth/request_token";
private static final String ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token";
private static final String AUTH_URL = "https://api.twitter.com/oauth/authorize";
private static final String PREFERENCE_FILE = "twitter_oauth.prefs";
private static CommonsHttpOAuthConsumer consumer;
private static CommonsHttpOAuthProvider provider;
private static String ACCESS_KEY;
private static String ACCESS_SECRET;
private Twitter twitter;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loginViaOAuth();
}
private void loginViaOAuth() {
try {
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
provider.setOAuth10a(true);
provider = new CommonsHttpOAuthProvider(REQUEST_URL, ACCESS_TOKEN_URL, AUTH_URL);
String authURL = provider.retrieveRequestToken(consumer, CALLBACK_URL);
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authURL)));
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
#Override
protected void onResume() {
super.onResume();
Uri uri = this.getIntent().getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
Log.d("verifier:", verifier);
try {
provider.setOAuth10a(true);
provider.retrieveAccessToken(consumer, verifier);
ACCESS_KEY = consumer.getToken();
ACCESS_SECRET = consumer.getTokenSecret();
AccessToken a = new AccessToken(ACCESS_KEY, ACCESS_SECRET);
// initialize Twitter4J
twitter = new Twitter();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
String tweet = "#OAuth working via android app!";
twitter.updateStatus(tweet);
Toast.makeText(this, tweet, Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
Just found out a possible solution: You need to set a Callback URL on your twitter application account.
I had exactly the same problem on my Android application. It was even more frustrating that my twitter login was perfectly working and starting to fail on the signature for some random reasons.
I ran a lot of tests and I found that the problem came from the Android browser which is used in the OAuth process:
if you are logging in using the stored login/password, or if you have a cookie with your Twitter session and just have to click on "Accept", it fill fail with the 401 error
if you manually delete and re-enter your password, then it works!
I still can't understand how this affects the API call, but I guess there is some mix up in the browser when you submit the "accept" form with pre-entered information.
I'd be very curious to see if my workaround solves also your problem. I understand this is not a proper solution, but this is a beginning.
EDIT: use http:// instead of https:// for the Twitter OAuth URLs and it solves the problem. I still don't unsertand what is happening...
Check your api request it must be .json or .xml, something like
https://api.jabbakam.com/network/get_list.json or
http://api.twitter.com/1/account/verify_credentials.xml
I advise you to use Scribe library there is a built in class for using Twitter API.
https://github.com/fernandezpablo85/scribe-java/blob/master/src/test/java/org/scribe/examples/TwitterExample.java
When you create your twitter keys did you make Access level: Read and write?