Integrating Twitter: why can’t I post status without having to authenticate? - android

I want to post a simple status message to a Twitter account that's linked to my app. All users of my app will post to the same Twitter account.
I've registered my app with Twitter (according to the guidance given here: How to post a tweet from an Android app to one specific account?) and I have the necessary ConsumerKey, ConsumerSecret, AccessToken and AccessTokenSecret. I've set the account to Read & Write, and set the REQUEST type to GET.
I'm using Twitter4J and installed the twitter4j-core-3.0.3.jar into my app. The Manifest file has the required "android.permission.INTERNET". This is the code …::
AccessToken a = new AccessToken(AccessToken, AccessTokenSecret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(ConsumerKey, ConsumerSecret);
twitter.setOAuthAccessToken(a);
try
{
twitter.updateStatus("Tweet Test #1");
Log.v(TAG, "Twitter Tweet sent!");
}
catch (TwitterException e)
{
// TODO Auto-generated catch block
Log.e(TAG, "Error sending Tweet:" + e.getMessage());
}
The twitter.updateStatus("xxxxxx") call causes an exception that reports “Received authentication challenge is null” in the logcat.
I assumed I could just post, but it seems Twitter wants something more?
Can anybody offer any advice as to what I'm doing wrong?

Managed to get to the bottom of this in the end!
My particular problem was that I didn't enter a Callback URL in the Twitter Apps setup page. I'm only interested in sending tweets to my app's linked Twitter account and I already have the 4 tokens/secrets, so I don't need to get the user to authorise thier own account via my app. As such, I don't need a Callback URL.
Unfortunately, the Twitter apps page lets you leave that field blank when you request the tokens/secret, and they don't make it clear that the Callback URL is a required field. If you don't need it, you can put absolutely anything you like in there; But if you leave it blank, Twitter won't let you tweet from your app! Setting the access type to "Read & Write" is good enough just to update status, but set it to "Read, Write & Direct Messages" if you want to do more.
Some tutorials say you should set the app type to "Browser" (instead of "Desktop"), but that option seems to have disappeared from the Twitter apps page, so I guess that's no longer important.
I managed to find some very good tutorials about tweeting from an Android app (here, here and, in particular, here) which make it clear that the Callback URL is a requirement, and go on to explain very clearly how to get it to work.

Have you checked to make sure there aren't any extra whitespaces in your Twitter4J configuration file? Double-check each of your consumer.. and access.. fields just in case.

Related

Stripe Connect Android

I am building an android app which pays someone. I explored Stripe Connect and it has an API and flow for website which requires displaying a stripe connect button and also needs a re-direct uri.
However, I can not find anything on for Android which allows to show a connect button and prompt user to create or connect a stripe account natively on Android device. Should I use a webview to do this or is there some more elegant way to do this?
As far as I know, there're no native SDK for mobile stripe connect. I did the webview route for android. Was trivial, but I agree that I wished there're a more elegant solution to this.
For those who are wondering on how to do this (since I can't find any doc out there), here you go:
Create a button that will open a new intent with a web view.
The webview should load the oauth/authorize endpoint ("https://connect.stripe.com/oauth/authorize?response_type=code&client_id=[YOUR_API_KEY]&scope=read_write"). Do not forget to enable javascript on your webview.
Once the form in webview is filled, Stripe will redirect to the redirect_url you provided with the authorization code if the authorization is successful. Have your web service parse that authorization code.
Have your service make a post to stripe oauth/token with providing grant_type, client_id, code, and client_secret.
Stripe will then respond back with the token (auth credentials) that you needed. Store that for later use & you're pretty much done.
I know this is quite an old one but, After investing my 2 days and trying, searching a lot of techniques to create stripe account in android platform I found Stripe API which solves my problem...
Firstly, here is the jar file you have to include in your project.
Then you have to initialize key provided you at the dashboard
Stripe.apiKey = "sk_test_xxxxxxxxxxxxxxxxxxxxxxxx";
RequestOptions requestOptions = RequestOptions.builder()
.setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxxx").build();
Map<String, Object> accountParams = new HashMap<String, Object>();
accountParams.put("managed", false);
accountParams.put("country", "US");
accountParams.put("email", "some#email.com");
To create an account use create() method for Account class with there parameter create link
try {
Account create = Account.create(accountParams, null);
} catch (AuthenticationException e) {
e.printStackTrace();
}
Then to retrieve account detail pass account id to retrieve() method
try {
Account detail = Account.retrieve("acct_xxxxxxxxxxxxxx", null);
} catch (AuthenticationException e) {
e.printStackTrace();
}
You can find more at Stripe Android API, and yes please do correct me if I am wrong..
Exception you need to catch AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException

Unity facebook API FB.API() java.io.FileNotFoundException

I ran into a problem. I am using Unity facebook SDK and FB.API to post on user feed.
The Code I am using is:
if (FB.IsLoggedIn) {
var data = new Dictionary<string, string>() {{"message", "I just scored "+score+" in GameName. Download it now!"}};
FB.API ("/me/feed", HttpMethod.POST,LogCallback, data);
}
void LogCallback (FBResult result) {
if (result.Error != null) {
print ("score submission failed with error= "+result.Error.ToString());
}
else {
print ("score submitted with result= "+result.Text.ToString());
}
}
and the result I am recieving is:
score submission failed with error= java.io.FileNotFoundException: https://graph.facebook.com/me/feed
I searched on the net and found that perhaps it has something to do with
publish_actions
permission. But I am using this permission.
I then went to the see the app on Facebook Developers and went to Status and Review.
There Approved permissions are:
email, public_profile and user_friends
I then tried to add
publish_actions
permission but it showed the error that I am missing icon, long description and privacy policy URL.
It wants to submit app for review. Am I doing everything correct? Do I need to upload android apk for them to review?
Can someone guide me? Thank you
You need to ask for publish_permissions from FB.Login first in order to use the /me/feed. (you'll see a message "this app wants to post on your behalf...")
That said:
Using the graph API to post a message to a user's feed if the user hasn't explicitly clicked a button indicating they want it to happen is known as 'implicit shares' and is discouraged.
Using a message you create in code instead of letting the user type in their own message is known as "pre-filling" and is not allowed by Facebook policies.
You don't need publish_actions if you use the Facebook dialogs instead of the graph API.

Android AccountManager, Oauth, getAccessToken() does nothing except ask for password

After spending roughly two days on this, I'm getting a little rattled. Although by now, having chosen OAuth2 over OpenID, I'm pretty well versed on the difference between Authenticate and Authorize.
I want my android app to provide several ways to authenticate users, one of them is google accounts, and later also facebook and twitter accounts. I'm trying to use the AccountManager class to get an OAuth access token to (for now) just verify the user's email address. The goal is that if the user already has a google account saved on the android device, they can authorize my app once, MAYBE even without typing a password, and never have to login again from their android.
I decided to use Google's own AccountManager as it promised to handle much of this natively in the Android framework, without even opening a browser window. I am using the library / build target for google APIs version 7 (Android 2.1), the first level that supports AccountManager.
I have tried this two different ways, one using AccountManager.getAuthTokenByFeatures() where you do not specify an Account object, and the other using getAuthToken() where you do specify such an object.
In each case, the call completes (as I expect it to) and the application displays an authorization dialog asking if I want to authorize the app. So far, so good. If I refuse, the program throws the exception that I expect. If I accept, a "Google sign-in" dialog appears asking me for the password to the account. Note that I already entered the password when I added the account to the device. If I type the password in the dialog, there is an "Authorizing" wait screen and then the same dialog re-appears. Oddly enough, the "Authorizing" wait screen seems to take a little bit longer if I type the CORRECT password. So it appears that I cannot get to the code path where I successfully obtain the token.
As tempting as it is to vent about google not being clear about what AUTH_TOKEN_TYPE is or the fact that the userinfo.email URL is undocumented, I would really just like to learn what I am doing wrong here and move past this.
Here is my code, I will monitor this and of course be happy to answer any questions. Right now I am going to work on getting a capture of the network traffic, to see if that provides any further insight into what is going wrong.
Here are images showing the auth screen (ok) and the password dialog (less ok)
http://imageshack.us/g/707/device20120609020618.png/
public void loginToGoogle() {
System.out.println("Starting");
AccountManagerFuture<Bundle> bundleFuture =
AccountManager.get(_activity).getAuthTokenByFeatures(
"com.google",
"https://www.googleapis.com/auth/userinfo.email",
null,
_activity,
null,
null,
new AccountManagerCallback<Bundle>() {
public void run(AccountManagerFuture<Bundle> future) {
Bundle bundle;
try {
bundle = future.getResult();
for (String s : bundle.keySet()) {
System.out.println("Found key: "+ s);
}
System.out.println(bundle.getString(AccountManager.KEY_ACCOUNT_NAME));
System.out.println(bundle.getString(AccountManager.KEY_AUTHTOKEN));
//Use Token
} catch (OperationCanceledException e) {
Log.e("e", e.getMessage(), e);
System.out.println("User appears to have denied auth request");
} catch (AuthenticatorException e) {
Log.e("e", e.getMessage(), e);
} catch (IOException e) {
Log.e("e", e.getMessage(), e);
}
}
},
null);
System.out.println("Done with AccountManager call");
}
Your issue most likely lies in the Auth Token Type you pass into the getAuthTokenByFeatures. Since you're using oauth2, you need to add it to beginning of your auth type:
String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/userinfo.email"
However, I'm not positive that the Auth Token Type you are trying to use is valid. I've only dealt with calendars and tasks myself. Hope this works out, if not, just let me know and I'll find you the right Auth type

pin in twitter4j

How can I get the pin for the twitter 4j ?
accessToken = twitter.getOAuthAccessToken(requestToken, pin);
please help..!
If you take a look to the twitter4J Code Examples, you see that the user is suppose to write the pin that he gets from the Twitter website where he will be redirected. If you want to take the pin from the URL without the user having to introduce it manually, I think the pin comes in the URL as "oauth_verifier". I did all the login with signpost because I didn't want the user having to write anything and I wasn't able to do it using only Twitter4J. Once I have the access token, I create the Twitter object from twitter4j with twitter = twitterfact.getOAuthAuthorizedInstance(consumerKey, consumerSecret,accessToken);and it works fine. This way it was much easier for me.
You need to redirect the user with a request token (that you can get from Twitter) to the twitter site, where the user enters the username/password and gets the pin back.
See e.g. https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/account/LoginActivity.java#L69 on how to direct to Twitter
and then https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/account/LoginActivity.java#L91 when the user has the pin and needs to proceed
You should be able to get away without using the PIN.
Twiiter4j DOC
Blog Post stating it is not required by Twitter

How to Login twitter using Oauth from my android application?

I am making an application which is a Twitter client. This means it connects to Twitter with OAuth. I register my application to Twitter and got all my keys, but now I do not have an idea how to connect my application with twitter. I have done some code mention below. Please help me out..
Twitter twitter=new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Consumer key, Consumer secret);
RequestToken requestToken = twitter.getOAuthRequestToken();
AccessToken accessToken=null;
Log.i("Acces Token",accessToken.getToken());
Log.i("Acces Tokensec",accessToken.getTokenSecret());
Thanks in advance.
You have two choices. Number one is easier. Number two is more difficult.
Number one just continues where you left off.
After you get a requestToken, you will need to launch a WebView and point the URL at requestToken.getAuthorizationURL(). The user will then log in and choose whether or not to allow access to his/her account. Next, if he/she hit allow, an access code will be displayed the user must copy/paste in your your own application. You will use that key with getOAuthAccessToken() (I think, I used the difficult way described later) to get the auth token that you should store somewhere permanently. At this point you are authenticated.
Number two also continues where you left off minus one detail...
twitter.getOAuthRequestToken(REDIRECT_URL).
That redirect_url must be set inside your twitter developer account first. Then follow the same steps as number one except that your webview needs to be customized. You need to use setWebViewClient() on your WebView and create a new class that extends WebViewClient. Inside the WebViewClient's onPageStarted, check if the URL starts with your return url. And then get the oauth info:
String oauth_token = uri.getQueryParameter("oauth_token");
String oauth_verifier = uri.getQueryParameter("oauth_verifier");
Use the oauth_verifier with twitter.getOAuthAccessToken() to get your token.
If the full OAuth web redirection is not convenient, you could try to use Twitter's xAuth service method to just convert a set of Twitter credentials to an OAuth access token (do that once, and save the token). Much easier on mobile applications, but you need to ask Twitter for permission to use xAuth by emailing api#twitter.com.
You could also check out another question on StackOverflow for more information on this.
If your problem is how to actually implement OAuth interactions, you might want to check out OAuth library information on Twitter and/or documentation on the library you are already using.

Categories

Resources