Forum Xamarin.Android

Tweet with image using Xamarin.Social

RonnyBattyRonnyBatty GBMember
edited September 2013 in Xamarin.Android

Hi there.

I'm attempting to call update_with_media on the Twitter REST API. After obtaining an authenticated account (_account var in the code below) via an OAuth1Authenticator, I do the following:

IDictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("status", "test tweet!");

Request request = MyTwitterService.CreateRequest(
    "POST",
    new Uri("https://api.twitter.com/1.1/statuses/update_with_media.json"),
    parameters,
    _account);

request.GetResponseAsync().ContinueWith(t => {
    if(t.IsFaulted) {
        // Oh oh, tweet failed.
    }
});

The Twitter API is responding with a 403.

I'm not even adding the image data yet, so that can't be the issue and if I replace the URL with a call to update.json, the Tweet is successfully posted, so there are no problems with permissions or the authenticated account.

The REST API docs for update_with_media says "...Your POST request's Content-Type should be set to multipart/form-data", so this MAY be the problem. Only trouble is, I have no way to set this header on the request. There are are no alternative CreateRequest methods on the TwitterService and nothing to set the contentType on the Request class itself.

Has anyone achieved this? Surely I can't be the only person trying to post Tweets with media on Android???

Cheers,
Ronny.

Posts

  • RonnyBattyRonnyBatty GBMember

    Ok, so I've given up on this. Xamarin.Social doesn't seem like a very complete solution to me, at least on Android.

    This sort of thing should be very straightforward so I'm now using an Intent to open the Twitter app. About 20 lines of code and I can now post both plain text and image Tweets, with no need to handle authentication myself.

    I can post my Intent code if anyone is interested.

  • Please do.

  • RonnyBattyRonnyBatty GBMember

    No problem. The following method works for both a plain Tweet and one with an image, just pass in an empty string as the imagePath parameter for text only Tweets.

    The "com.twitter.android" string makes sure that only the official Twitter app is opened by the Intent, you can change this to make it less strict and include other Twitter apps if you like.

    public bool Tweet(string text, string imagePath) {
        Intent tweetIntent = new Intent(Intent.ActionSend);
        tweetIntent.PutExtra(Intent.ExtraText, text);
        if(imagePath != null && imagePath.Length > 0) {
            tweetIntent.PutExtra(Intent.ExtraStream, Uri.FromFile(new File(imagePath)));
            tweetIntent.SetType("image/jpeg");
        }
        else {
            tweetIntent.SetType("text/plain");
        }
    
        PackageManager pm = _activity.PackageManager;
        IList<ResolveInfo> resolvedInfoList = pm.QueryIntentActivities(tweetIntent, PackageInfoFlags.MatchDefaultOnly);
    
        foreach(ResolveInfo resolveInfo in resolvedInfoList) {
            if(resolveInfo.ActivityInfo.PackageName.StartsWith("com.twitter.android")) {
                tweetIntent.SetClassName(resolveInfo.ActivityInfo.PackageName, resolveInfo.ActivityInfo.Name);
                _activity.StartActivity(tweetIntent);
                return true;
            }
        }
        return false;
    }
    
  • CheesebaronCheesebaron DKInsider, University mod

    Hmm this is not the best way to do it. This forces the user to have the official Twitter client, which does not fulfill the needs of every Twitter user. Hence, why there are so many alternatives. This means that not every Twitter user will be able to use the Tweet function in your app.

    Have you looked at how Xamarin.Social deals with images? If you look at the code from line 75 in TwitterService, it shows how images are added, which are a part of Item. I would dig deeper and do the tweeting all inside my app, or support all the popular clients.

  • RonnyBattyRonnyBatty GBMember

    I spent a long time looking at Xamarin.Social and scouring the forums for a solution to my original post so I was happy to come up with anything that worked. The sample project supplied with Xamarin.Social should really give an example of posting a Tweet with an image, instead of having to look through the source code.

    I agree, that forcing the user to have the Twitter app installed is not ideal but it works, and looks infinitely better than the ShareUI provided by Xamarin.Social. Also, the StartsWith string comparison in my code could be less strict and an Intent chooser used to present a range of client apps that could fulfil the request.

  • CheesebaronCheesebaron DKInsider, University mod

    If you want to see things change in Xamarin.Social I suggest you make some issue request on the GitHub page. It is an open source project which you can, if you want, put up suggestions, fork the project, make changes etc.

    ShareActivity does contain code, showing how to attach images to a tweet, and nothing prevents you to create your own.

  • RonnyBattyRonnyBatty GBMember

    Yes, but you're working under the assumption that everyone downloading Xamarin.Social is doing so from GitHub and is a potential "developer" of the component.

    I downloaded it from the components page, which contains a solution with pre-compiled .dll files and basic sample code. As a "user" of the component, I wouldn't really expect to have to concern myself with the code inside Xamarin.Social.Android.dll

  • CheesebaronCheesebaron DKInsider, University mod

    My opinion is, if you are just a little bit serious about developing stuff and there is no documentation available for the component, yet. Then you are ought to dig a bit in the code. I am not developing the Xamarin.Social component, and it took me less than 5 minutes to find the code that does the image attachment.

  • RonnyBattyRonnyBatty GBMember

    I agree with your opinion and appreciate the pointer to the code. To be honest, digging around in the component code hadn't crossed my mind until several hours of fruitless searching on the forums had passed. At which point I had started to consider the alternatives to Xamarin.Social, as the clock was ticking.

    My frustration is really with the component documentation. If a component claims to "Share statuses, links, and images on Facebook, Flickr, Twitter and more" and has a 4 star rating, I expect to be able to achieve this without digging around in its source code to work out how.

    Anyway, based on your GitHub pointer, here's a slight tweak to the Share method in the Xamarin.Social sample project. This will post a Tweet with the icon resource as an image, haven't tried it with Facebook though:

    IList<ImageData> images = new List<ImageData>();
    images.Add(new ImageData(BitmapFactory.DecodeResource(this.Resources, Resource.Drawable.Icon)));
    
    Item item = new Item {
        Text = "I'm sharing great things using Xamarin!",
        Images = images,
        Links = new List<Uri> {
            new Uri ("http://xamarin.com"),
        },
    };
    
  • RonnyBattyRonnyBatty GBMember

    Additionally, the SoMA sample may also be a useful resource when using Xamarin.Social (https://github.com/xamarin/mobile-samples/tree/master/SoMA).

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    @RonnyBatty Your last post was more than 1 year old. Is your twitter posting app still working using Xamarin.Social? When you posted this probably Xamarin.Forms was not there. Have you used Xamarin.Social with Xamarin.Forms?

    In my XF's Android project, I am trying to use Xamarin.Social to post tweets. Unfortunately it works till showing the Twitter interface. But it is asking to "Add Account" to send from. By tapping on the link it is crashing. Any idea how to make it work?

  • RonnyBattyRonnyBatty GBMember

    Just looked at my code and I'm using an Intent so I assume I couldn't get the Xamarin.Social component to work in the way I wanted or just wanted to keep it nice and simple.

    I posted the Intent code earlier in this thread if you want to try it but does require the Twitter client being installed on the phone to work. I guess my reasoning is that if someone wants to post from an app to Twitter, there's a very high chance they will already have the Twitter app on their phone or be happy to install it.

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    I will try your 'intent' method.

    As you pointed out one should have the Twitter app installed. But there are cases like me, as such many of the developer tribe, who may have multiple devices for testing. I don't want to dismiss the notification on each of them (that's too much of a pain). But however I have installed Twitter on my GS4 while trying the Social component. Of course it didn't work as I mentioned above.

    Sometime back it was quite simple to do this on Xamarin, but now I don't find those API. In XF's Windows Phone project, I used Hammock successfully. That opens a Web Page provided by Twitter for login info. Though it was not simple in XF to get it to work, I felt it was more straight forward.

  • RonnyBattyRonnyBatty GBMember

    If you want to make sure the actual post works on several devices then just create another Twitter account for development purposes and you can post as much as you like without annoying any followers.

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    @RonnyBatty Thanks. Your Intent method works. I am going to keep it that way.

Sign In or Register to comment.