OAuth + Twitter on Android: Callback fails - android

My Android application uses Java OAuth library, found here for authorization on Twitter. I am able to get a request token, authorize the token and get an acknowlegement but when the browser tries the call back url to reconnect with my application, it does not use the URL I provide in code, but uses the one I supplied while registering with Twitter.
Note:
1. When registering my application with twitter, I provided a hypothetical call back url:http://abz.xyc.com and set the application type as browser.
2. I provided a callback url in my code "myapp" and have added an intent filter for my activity with Browsable category and data scheme as "myapp".
3. URL called when authorizing does contain te callback url, I specified in code.
Any idea what I am doing wrong here?
Relevant Code:
public class FirstActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OAuthAccessor client = defaultClient();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(client.consumer.serviceProvider.userAuthorizationURL + "?oauth_token="
+ client.requestToken + "&oauth_callback=" + client.consumer.callbackURL));
startActivity(i);
}
OAuthServiceProvider defaultProvider()
{
return new OAuthServiceProvider(GeneralRuntimeConstants.request_token_URL,
GeneralRuntimeConstants.authorize_url, GeneralRuntimeConstants.access_token_url);
}
OAuthAccessor defaultClient()
{
String callbackUrl = "myapp:///";
OAuthServiceProvider provider = defaultProvider();
OAuthConsumer consumer = new OAuthConsumer(callbackUrl,
GeneralRuntimeConstants.consumer_key, GeneralRuntimeConstants.consumer_secret,
provider);
OAuthAccessor accessor = new OAuthAccessor(consumer);
OAuthClient client = new OAuthClient(new HttpClient4());
try
{
client.getRequestToken(accessor);
} catch (Exception e)
{
e.printStackTrace();
}
return accessor;
}
#Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
Uri uri = this.getIntent().getData();
if (uri != null)
{
String access_token = uri.getQueryParameter("oauth_token");
}
}
}
// Manifest file
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".FirstActivity"
android:label="#string/app_name">
<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:scheme="myapp"/>
</intent-filter>
</activity>
</application>

Twitter does not honor callbacks requested in OAuth requests (Twitter API Announce) and will only redirect to the callback URL specified in the Application Settings (note that "localhost" is not allowed).
I assume you checked Oauth-callback-on-android question.
Android guesswork--
After a bit of reading up, I see Android browser redirects MyApp:/// to your application and I'm guessing Twitter doesn't like this bespoke URI prefix. I'm no android developer but one suggestion I might make is to get "www.myapp.com" on the web and have a re-redirect there.
So have your OAuth return to http://www.myapp.com/redirect.aspx?oauth_token=abc and have that page redirect to myapp:///oauth_token=... (the desired result)

In my case, i have this working:
String authURL = m_provider.retrieveRequestToken (m_consumer, CALLBACK_URL);
And in the Manifest:
<activity android:configChanges = "keyboardHidden|orientation" android:name = "xxxx.android.xxxxx">
<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:scheme="myapp" android:host="tweet" />
</intent-filter>
In this case the callback url will be: myapp://tweet

It looks to me like you're doing the correct thing and Twitter is screwing up by always accepting your registered callback URL. Is there any way to change the registered URL? Maybe you could re-register and try an Android callback next time, see what happens.

My problem was that I was trying to log in with the same account I made the Twitter app. After I logged in with my personal profile the call back works (so far).

Related

Fit Bit Login, using Chrome Custom Tabs, no call back in my Application

I am trying to integrate FIT BIT login in my android application .
but i am not getting any call back in my application when the login is successful
I am using this reference fit bit link
As in the document we cannot use web view so i used Chrome Custom Tabs .
this is my redirect URL = "http://52.9.44.227:3000/feed"
which is returned from the url
this is my manifest for getting call back
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<data
android:host="52.9.44.227:3000"
android:pathPrefix="/feed"
android:scheme="http" />
</intent-filter>
</activity>
This is my java code to launch the web URL by fit bit.
void launchTab(final Context context, final Uri uri){
final CustomTabsServiceConnection connection = new CustomTabsServiceConnection() {
#Override
public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient client) {
final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
final CustomTabsIntent intent = builder.build();
client.warmup(0L); // This prevents backgrounding after redirection
intent.launchUrl(context, uri);
}
#Override
public void onServiceDisconnected(ComponentName name) {
}
};
CustomTabsClient.bindCustomTabsService(context, "com.android.chrome", connection);
}
this is the URL is trying hit for fit bit login
fit bit login link

How to use facebook mobile hosting api to create app link in facebook sdk android

I've preferred the below link to create a link object and app link but unfortunately i could not able to find anything from this link
Mobile Hosting Api. So please guys can you please tell me the exact steps to create the app link for my mobile application.
Here is the manifest code for the activity
<activity android:name=".AppLinkActivity"
android:label="#string/app_name" >
...
<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:scheme="com.GoCarDev" />
</intent-filter>
</activity>
and here is the Activity on which i have to redirect the app link
FacebookSdk.sdkInitialize(this);
Uri targetUrl =
AppLinks.getTargetUrlFromInboundIntent(this, getIntent());
Toast.makeText(this, "Link" + targetUrl, Toast.LENGTH_SHORT).show();
if (targetUrl != null) {
Log.i("Activity", "App Link Target URL: " + targetUrl.toString());
} else {
AppLinkData.fetchDeferredAppLinkData(
this,
new AppLinkData.CompletionHandler() {
#Override
public void onDeferredAppLinkDataFetched(AppLinkData appLinkData) {
//process applink data
}
});
}
Thank you guys in advance

Android redirect uri

I'm working with the instagram API and I don't understand what I should put in for the redirect api. At the moment I simply put in https://127.0.0.1
But I dont really understand. Also, once I get the allow/cancel screen and I press allow it gives me an error saying that I cant go to that address but I can also see the authorization code appended on to the address. So how can i redirect back from my redirect uri? How can I tell android that after user clicks allow to come back into the app use the code for further authentication?
Im sure you will say something like make my own custom scheme/ intent filters etc but please be a little more supportive im new and I dont understand and I did do research on them.
My on resume method is below
#Override
protected void onResume() {
super.onResume();
// the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(redirectUri)) {
// use the parameter your API exposes for the code (mostly it's "code")
String code = uri.getQueryParameter("code");
if (code != null) {
// get access token
LoginService loginService =
ServiceGenerator.createService(LoginService.class, clientId, clientSecret);
AccessToken accessToken = loginService.getAccessToken(code, "authorization_code");
} else if (uri.getQueryParameter("error") != null) {
// show an error message here
}
}
}
This is my manifest snippet dealing with intent filters:
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
<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="redirecturi"
android:scheme="your" />
</intent-filter>
</activity>
You have to setup an event listener on the browser view and check for code in URL param if the URL is equal to the redirect_uri, and then make POST request to the auth URL using the code and client_secret as documented in Instagram authentication
something like this:
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
String code = url.getQueryParameter("code");
// ...
}
});

Android return from browser to app

I have option in my app to start browser and load imdb website.
I'm using ActionView for this.
Intent intent1 = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(website));
try {
activity.startActivity(intent1);
} catch (Exception e) {
Toast.makeText(activity, R.string.no_imdb, Toast.LENGTH_SHORT)
.show();
}
The problem occurs when I tap on back button.
When default browser app is launched everything is ok.
When Opera Mini app is launched, when I tap on back button, it seems like my app receives two back actions, and finish my current activity.
How to prevent this?
Try starting the intent in a new task:
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Or
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Please add this code to your android manifest for activity that you need return
<activity
android:name="YourActivityName"
android:launchMode="singleTask">
<intent-filter>
<action android:name="schemas.your_package.YourActivityName" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.ALTERNATIVE" />
</intent-filter>
</activity>
and add this to your web page
click to load app
because only one app has this action name (schemas.your_package.YourActivityName) on your phone, web page directly return to app
Also You can Use Airbnb DeepLink lib
Example
Here's an example where we register SampleActivity to pull out an ID from a deep link like example://example.com/deepLink/123. We annotated with #DeepLink and specify there will be a parameter that we'll identify with id.
#DeepLink("example://example.com/deepLink/{id}")
public class SampleActivity extends Activity {
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent.getBooleanExtra(DeepLink.IS_DEEP_LINK, false)) {
Bundle parameters = intent.getExtras();
String idString = parameters.getString("id");
// Do something with idString
}
}
}

How to check callback_url in twitter application

i have loads of examples for O_Auth but no one is working properly and i guess the only thing is callback URL in every example, I am getting 401 error like consumer key or signature didn't match. I already seen so many examples on 401 the only thing that goes into my favor is my callback url.
android app cannot connect to twitter.
Getting 401 when requesting access token with signpost within android with-signpost-within-android
Android Dev - Callback URL not working... (0_o)
I already seen all these examples and many more.
But still i am not getting my point, If at the time of application form i use http://www.meomyo.com at callback url, Then what should i use it in my coding
like android:scheme="?" android:host="?"
currrently i am using other examples callbacks
//private static final Uri CALLBACK_URI = Uri.parse("bloa-app://twitt");
private static final Uri CALLBACK_URI = Uri.parse("twitterapp://connect");
i have consumer key as well secret key, but in case of callback url i am stuck on it.
If anyone want i can provide my consumer as well secret key.
public class OAuth extends Activity {
private static final String APP = "OAUTH";
private Twitter twitter;
private OAuthProvider provider;
private CommonsHttpOAuthConsumer consumer;
private String CONSUMER_KEY = "Xh3o8Gh1JQnklkUnTvvA";
private String CONSUMER_SECRET = "SCLy6yoUSth53goAsYYkoqR4ZuBoaInyJXsm5PQR11I";
private String CALLBACK_URL = "merabharatmahan://piyush";
private TextView tweetTextView;
private Button buttonLogin;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tweetTextView = (TextView)findViewById(R.id.tweet);
buttonLogin = (Button)findViewById(R.id.ButtonLogin);
buttonLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
askOAuth();
}
});
}
/**
* Open the browser and asks the user to authorize the app.
* Afterwards, we redirect the user back here!
*/
private void askOAuth() {
try {
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
provider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token",
"http://twitter.com/oauth/access_token",
"http://twitter.com/oauth/authorize");
String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show();
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
} catch (Exception e) {
Log.e(APP, e.getMessage());
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
/**
* As soon as the user successfully authorized the app, we are notified
* here. Now we need to get the verifier from the callback URL, retrieve
* token and token_secret and feed them to twitter4j (as well as
* consumer key and secret).
*/
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
try {
// this will populate token and token_secret in consumer
provider.retrieveAccessToken(consumer, verifier);
// TODO: you might want to store token and token_secret in you app settings!!!!!!!!
AccessToken a = new AccessToken(consumer.getToken(), consumer.getTokenSecret());
// initialize Twitter4J
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
// create a tweet
Date d = new Date(System.currentTimeMillis());
String tweet = "#OAuth working! " + d.toLocaleString();
// send the tweet
twitter.updateStatus(tweet);
// feedback for the user...
tweetTextView.setText(tweet);
Toast.makeText(this, tweet, Toast.LENGTH_LONG).show();
buttonLogin.setVisibility(Button.GONE);
} catch (Exception e) {
Log.e(APP, e.getMessage());
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}
manifest code is
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".OAuth"
android:label="#string/app_name"
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:scheme="merabharatmahan" android:host="piyush" />
</intent-filter>
</activity>
</application
now i am getting communication with the service provider is failed: Received Authentication challenge is null. It is the simplest example that i put here.
twitterapp://connect is your call back url.
In Android manifest.xml define your activity like this:
<activity android:name=".auth.SocialNetworkActivity"
android:configChanges="orientation"
android:label="Connect SNS" android:launchMode="singleTask">
<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:scheme="twitterapp" android:host="connect" />
</intent-filter>
</activity>
Now, it will open twitter web page, and on successful authentication, your activities onNewIntent() callback method will be called.
In above example, use your own activity name.
Apart from that, twitter integration in my application is done using twitter4j.
You must put whatever you think will be unique:
android:scheme="name-of-your-dog" android:host="something-else"
Oh, wait... maybe there's someone else owning a dog called like yours... so, use the package name of your app:
android:scheme="com.your.package" android:host="something-else"
And, if it's not working for you... I don't think it's related to the callback URL. How are you declaring your activity in the manifest? You added the android:launchMode="singleTask" parameter, right?
And, as the zombie guy pointed out, you must handle the callback in the onNewIntent method of your auth activity. Something like this:
final Uri uri = intent.getData();
if (uri != null && uri.getScheme().equals("name-of-your-dog")) {
//etc...
}

Categories

Resources