Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I'm looking for a way to use this to upload photos to Twitter that have been attached to nodes. I believe this is possible per this item in Twitter's documentation pages:
https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media
Is anyone doing this already? Being able to add a photo and not just a link to the node would be a great advancement.
My team would be willing to work with anyone who is already working on this, or we may be able to tackle it ourselves.
Thanks.
Comment | File | Size | Author |
---|---|---|---|
#60 | interdiff-1981472-54-60.txt | 3.69 KB | oadaeh |
#60 | twitter-support_media_upload_endpoint-7x6x-1981472-60.patch | 16.92 KB | oadaeh |
| |||
#59 | twitter-support_media_upload_endpoint-7x5x-1981472-59.patch | 17.28 KB | oadaeh |
| |||
#49 | interdiff-1981472-48-49.txt | 2.12 KB | dmsmidt |
#49 | Support_media_upload_endpoint-1981472-49.patch | 14.4 KB | dmsmidt |
|
Comments
Comment #1
jmrivero CreditAttribution: jmrivero commentedtwitter.lib.php has a statuses_update_with_media but there is a "@TODO support media at TwitterStatus class." in it, looking at twitterstatus class the support for media is not implemented yet.
Comment #2
jmrivero CreditAttribution: jmrivero commentedWell, I think I got to handle media in TwitterStatus class by adding the following to the __construct:
This will add an array to the status object containing the hashtags, media, urls and user_mentions as described in the response in https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media
Then the images attached can be accesed in the statuses with:
This is just some hack in the air, the module doesnt use this entities and I am still trying to figure how to post to twitter with media, I have tryed to set the headers to multipart/form-data if the media param is set in request of the Twitter class like this but it doesnt seem to work so far:
Then added a function in twitter.inc to set a status with media:
Dosnt work so im just sharing some ideas while toying with it, hope it helps.
Comment #3
jmrivero CreditAttribution: jmrivero commentedComment #4
jmrivero CreditAttribution: jmrivero commentedUsing tmhOauth while this feature is implemented calling this function with rules on each cron run:
Comment #5
drasgardian CreditAttribution: drasgardian commentedHere is a patch that adds support for media in the TwitterStatus class.
It also fetches the configuration settings from twitter (as per https://dev.twitter.com/rest/reference/get/help/configuration) to verify how many images can be uploaded. So when twitter starts accepting more than one image this will start allowing more than one image to be sent in the request.
The helper function current_configuration() might be useful elsewhere.
Comment #6
errand CreditAttribution: errand commentedI've installed dev version of the module.
What else should i do to make it post images too?
Comment #7
DamienMcKennaTriggering the testbot.
Comment #8
DamienMcKennaTriggering the testbot.
Comment #9
jwilson3Patch looks good so far, but it only really adds the media support to the
twitter.lib.php
which I haven't honestly tested yet because as far as I can tell, thestatuses_update_with_media()
function is never called anywhere throughout the module's codebase. So, there is more work to be done here, and the question is how exactly to integrate the media functionality into the Twitter module's existing features?I think the integration should be two-fold:
1) integrate with Content-type based
twitter_post
submodule (admin/config/services/twitter/post):For each content type in the list that is enabled:
* Allow admin to pick which image style to use, and
* Provide a way to select which image field to use, if there are multiple image fields.
* Investigate and test Twitter support for multiple media files (mentioned above) using Drupal image fields with multiple cardinality.
2) integrate with the Rules-based
twitter_actions
submodule:* Create a new action called "Post a message with media to Twitter".
* Works the same way as existing "Post a message to Twitter" action, but in addition to the "Message" parameter, accepts a second "Media" parameter.
* Investigate support for multiple media files (mentioned above) using Drupal image fields with multiple cardinality.
Comment #10
sarhugo CreditAttribution: sarhugo commentedAlso the statuses/update_with_media endpoint has been DEPRECATED.
It should first upload the media through media/upload and then attach the media's id to the status.
Comment #11
MickC CreditAttribution: MickC commentedPatch not working for me - instead of an image I see text only of 'Property' + the field name, in my case - "Property field_tweet_image"
Comment #12
intrafusionSeeing as the statuses/update_with_media endpoint has been deprecated, I have started work on using the media/upload endpoint instead.
Here is a patch the start the process which does the following:
1. Defines TWITTER_UPLOAD constant of https://upload.twitter.com
2. Allows twitter_upload variable to be edited on admin/config/services/twitter/settings
3. Creates new function twitter_upload_media to facilitate upload
4. Several updates to twitter.lib.php which actually uploads the media, a lot of the changes were taken from the changes in #5
To use these changes you will need to manually code them via:
The next stage (assuming that this is OK) would be to integrate this into twitter_set_status function as the returned media id can be used in the media_ids parameter of the statuses/update endpoint
Comment #13
DamienMcKennaClosed a duplicate: #2128313: Post images in a tweet
Comment #14
DamienMcKenna@intrafusion: Thanks for working on this. I think it'd be useful to be able to use a token / selector to indicate what field to use - how often do people want to upload an image to Twitter that isn't already in their node?
Comment #15
intrafusion@DamienMcKenna I totally agree but think we need full token support as per #1441596: Add support for tokens module on twitter post. before we can do that.
My work up till now was handling the actual posting to Twitter.
Comment #16
intrafusionComment #17
intrafusion@DamienMcKenna, upon further work I'm not sure how easy this will be to do.
Firstly, to be able to pull the correct value via a token we need Entity Tokens (part of Entity API).
Secondly, #2165511: Multi-value File Field Token with :file property is empty means that if you have a multi-value field rather than using [node:field-image:file], you need to use [node:field-image:0:file], [node:field-image:1:file], etc.
Thirdly, the file tokens comprise of fid, name, mime, size, url, timestamp & owner values meaning that fid is the only really usable token meaning the user would need supply [node:field-image:file:fid] or [node:field-image:0:file:fid], [node:field-image:0:file:fid], etc. which seems to open a whole realm for mistakes, etc.
Wondered whether you had any additional ideas how this could be accomplished?
Comment #18
doitDave CreditAttribution: doitDave commentedI consider media handling (and doing it with full media module benefits) a major item that will also add much of even more value to the module.
I would like to suggest an even more comfortable improved/advanced Twitter post field (see also #2474157: Add non-"hidden" field formatter for "Twitter field") or rather field collection, maybe even overridable in order to use arbitrary media selectors like MBP, IMCE or whatever. Media selected there will first be uploaded and then either mapped to an entered placeholder or, if that is not present, simply appended to the tweet (which is eventually shortened).
What do you think?
Comment #19
Andre-Bjust an idea: why not expose a configurable token with this module? e.g. add a setting on image fields to expose them in a way they are addressable for twitter posting? And also state there that the nth element will be taken (e.g. first,). that way we could add logic ourselves?
Comment #20
petrovnn CreditAttribution: petrovnn commentedintrafusion, in what file needed is to write the code? In the module file?
from your comment #12.
patch applied successfully
Comment #21
intrafusion@petrovnn you will need to use this code in a custom module. For my instance I was taking newly uploaded media and posting it to Twitter, so I used hook_node_insert($node) & hook_node_update($node), but I simplified the example code I posted
Comment #22
petrovnn CreditAttribution: petrovnn commentedso, my code looks like:
but i have this error:
what can you recommend?
Posting with pictures is very necessary.
It can be bad patching?
what program do you recommend for patches?
Line, which causes an error:
in function:
Google gave me two links
http://stackoverflow.com/a/54572
http://stackoverflow.com/a/14242492
Comment #23
intrafusionLooks like:
Is not returning an object, is the account name correct?
Comment #24
petrovnn CreditAttribution: petrovnn commentedyes.
so, database logging noticed:
But i have authenticated account: http://i.imgur.com/82sPQoE.png
and have registered twitter application http://i.imgur.com/yZLDIrn.png
maybe me need to learn how correctly configure twitter module?
but before patching I have already published tweets (without pictures) without any problems with these same settings: http://i.imgur.com/GKgcDDl.png
Comment #25
petrovnn CreditAttribution: petrovnn commentedI wanted to report some results.
1. The patch could NOT be applied correctly (for the module version 7.x-5.x stable)
PHPStorm:
GIT for windows:
2. The most of the problems caused file twitter.lib.php
3. I applied the patch manually, line by line, the result is: http://pastebin.com/iXqL7r0A
(not copied some php comments)
4. variables from my_module are correct:
5. No errors in dblog
no errors in apache2 error.log
no results after node_save (Posts are not published on twitter at all)
6. patch may be a little outdated?
7. may be necessary to apply the patch to version 7.x-6.x or DEV?
sorry for such a long comment, but I really need to make post pictures on Twitter
Comment #26
intrafusionOK my patch was created against the dev version prior to 7.x-5.9 and there have been fundamental changes to the Twitter module since then.
However, there are bigger problems see #1981472-17: Support media/upload endpoint & #1441596: Add support for tokens module on twitter post.
Comment #27
intrafusionRe-rolled patch from #1981472-12: Support media/upload endpoint against latest dev
Comment #28
DamienMcKennaIf the API changes work correctly the question then is how to determine a) whether images should be uploaded, b) which images should be uploaded? It shouldn't just automatically upload every image it finds, because thanks to field defaults there could be lots of duplicates.
Comment #29
petrovnn CreditAttribution: petrovnn commentedintrafusion Excuse me, I not quite understand English, did not understand what it means:
for which version of the module should be applied your patch "Support Media_upload Endpoint-1981472-26.patch"?
Unfortunately, I am not a programmer, and I can not just pick up and fixed a tokens issue in this module.
Comment #30
petrovnn CreditAttribution: petrovnn commentedI'm not quite understand what it means:
This means i need more changes in some files?
How can i integrate "this into twitter_set_status" function?
from here #12
Comment #31
intrafusion@petrovnn the patch should apply cleanly to both versions, but 7.x-5.x-dev was what the patch was created against.
The changes which have been made so far are for the API, as mentioned in #1981472-28: Support media/upload endpoint we need to decide what happens next.
Comment #32
petrovnn CreditAttribution: petrovnn commentedBut what should I do next? I do not understand.
The patch was applied successfully, but neither stable nor dev version does not publish pictures to Twitter (publication in Twitter works, but there is no image in the post).
Can-I you somehow help?
Or is it a dead end?
Can I hack, any file or function to get the publication of pictures?
I understand that you want a "clean", "academic" way to upload pictures, but I fear that this will have to wait too long. Is there any faster way to get this functionality from one field as you described here:
How should I change function twitter_set_status() to attach variable
$media
to the variable$status
?\sites\all\modules\twitter\twitter.inc
Comment #33
intrafusion@petrovnn You don't need to change anything else.
$media = twitter_upload_media
will return an array and$media['media_id']
is what you need:$status
is the actual 140 characters for the tweet, but you will need to allow approx. 10 characters for each image uploaded and you can only upload 4 images for each tweetComment #34
petrovnn CreditAttribution: petrovnn commentedbut where I need to insert this code?
In my module in hook_node_insert (), or in the function twitter_set_status () in file twitter.inc ?
If I add a variable $media to the function twitter_set_status (), there is a message that the variable $media undefined.
I need only one picture in a tweet, can-I to write so?
I found only one place where there is a line
$ twitter-> statuses update ($ status
in file twitter.inc, function twitter_set_status ($ twitter_account, $ status):
but if i change last string to:
PHP return undefined error for variable $media.
At the same time, in a file module function hook_node_insert (), the variable $media exists, and here it is:
but tweets still posting without images:
... and I really do not understand what I'm doing wrong.
Comment #35
cloudbull CreditAttribution: cloudbull commentedhi,
this patch already merged ? Or i still have to patch it while using the 6.x update ?
Comment #36
cloudbull CreditAttribution: cloudbull commentedi see there is a function in the patch: twitter_upload_media but never being called. Is that the code updated or ? and tested 5.x-dev with the patch and not working.
Keith
Comment #37
cloudbull CreditAttribution: cloudbull commentedi just made a success image upload by hacks below
/**
* Post a message to twitter
*
* @param $twitter_account
* object with a Twitter account.
* @param $status
* string message to publish.
* @return
* array response from Twitter API.
*/
function twitter_set_status($twitter_account, $status) {
$twitter = twitter_connect($twitter_account);
if (empty($twitter)) {
return FALSE;
}
// Support media
global $user;
$temp = cache_get('temp_media_ids_' . $user->uid);
$param = array();
if ( $temp ) {// if there is a cache from same user
$param['media_ids'] = $temp->data['media_id_string'];
cache_set('temp_media_ids_' . $user->uid, ''); //Remove the image after use it
}
return $twitter->statuses_update($status, $param);
}
Comment #38
cloudbull CreditAttribution: cloudbull commentedand support insert image when unpublish / publish content, the way to pass media id i use is by cache, is there any better approach?
worthy to patch this into twitter module ?
+// implement hook_node_update for auto-sent twitter
+function coconuts_solr_node_update($node) {
+
+ //If publish a content from unpublish
+ if (isset($node->original->status) && $node->original->status == 0 && $node->status == 1){
+
+ if ( $node->field_image['und'][0]['fid'] ) {
+ $file = file_load($node->field_image['und'][0]['fid']); // The image which needs uploading
+ $path = drupal_realpath($file->uri);
+ $handle = fopen($path, "r");
+ $contents = fread($handle, filesize($path));
+ fclose($handle);
+ }
+
+ module_load_include('inc', 'twitter');
+ if ( $node->twitter['account'] )
+ $account = twitter_account_load($node->twitter['account']); // The twitter account name or id
+ if ( $account )
+ $media = twitter_upload_media($account, $file->filename, $file->filemime, $contents);
+
+ global $user;
+ //Set as cache for twitter module to use
+ cache_set('temp_media_ids_' . $user->uid, $media);
+
+ }
+}
Comment #39
Jerenus CreditAttribution: Jerenus as a volunteer commentedThis is a patch to fix the image upload issue. It was build on top of 7.x-5.x-dev. Please help checking. Thanks
Comment #41
sarathkmWhat I assume is this patches is not necessary if you are trying to implement to post your new content with images. That twitter does automatically based on markup tags for twitter. You can provide the meta information using metatag module. Provide appropriate information and see the magic happen in your twitter feeds as you see in your facebook page feed when you post article when it has Organic Graph information in its meta.
For more information on twitter card markup : Click Me
Comment #42
GuyPaddock CreditAttribution: GuyPaddock at RedBottle Design, LLC for Inveniem commented#27 does not apply cleanly to 7.x-6.x-dev, FYI.
Comment #43
GuyPaddock CreditAttribution: GuyPaddock at RedBottle Design, LLC for Inveniem commented@Jerenus: How is your patch in #39 better / how does it improve upon the patch in #27?
Comment #44
GuyPaddock CreditAttribution: GuyPaddock at RedBottle Design, LLC for Inveniem commented@intrafusion: In #33, why
implode(',', $media['media_id'])
? The media ID isn't an array, as far as I can tell.Comment #45
dmsmidt@GuyPaddock, I compared 26 and 38, the only functional change is in twitter_set_status(). It adds:
It seems it is the hacky fix proposed by @cloudbull in #37 and #38 and IMHO should not go in like this.
Comment #46
intrafusion@GuyPaddock, it's possible to upload 1-4 images for each tweet, so by making sure it's always an array will handle cases when there is only 1 image as well as multiple.
Comment #47
dmsmidtSince there is no upgrade path for 5.x to 6.x I'm working on some improvements of the patch in #26/#27.
Among some coding standards cleanup I'll introduce a hook in the twitter_post module, to more easily provide media to upload with a status update.
Comment #48
dmsmidtSo here is an improved patch, which makes it easier to provide media programatically during a node insert. Just implement
hook_twitter_post_node_insert_files
(see the new twitter_post.api.php file).Comment #49
dmsmidtProcessed IRL review from @stevenbuteneers.
Comment #50
Steven Buteneers CreditAttribution: Steven Buteneers commentedComment #51
RAFA3L CreditAttribution: RAFA3L commentedThanks dmsmidt, work perfect after fix this in the twitter.inc file:
line 290
$raw_media = file_get_contents($file['uri']);
line 297
$response = $twitter->media_upload($file['filename'], $file['filemime'], $raw_media);
Comment #52
DamienMcKennaComment #53
smustgrave CreditAttribution: smustgrave commentedDoes anyone know if this patch would work for the 7.x-6.x dev version?
Comment #54
oadaeh CreditAttribution: oadaeh at Hook 42 commented@RAFA3L I think you're using it wrong. $files is supposed to be an array of Drupal file objects.
The comment for the $files parameter for twitter_set_status_with_media() is not very clear on that.
@smustgrave the patch does NOT apply to the 7.x-6.x-dev branch. However, you can try this one I manually created from the one for 7.x-5.x. I have not tested the code, and I really didn't inspect it very closely.
There has been a change to the 7.x-6.x-dev branch that has not yet been applied to the 7.x-5.x-dev, so there are a couple of differences between the two patches for this issue.
Comment #55
oadaeh CreditAttribution: oadaeh at Hook 42 commentedComment #56
smustgrave CreditAttribution: smustgrave commented@oadaeh Thanks for providing that patch. I tried it out and I get the following errors. I'm trying to test this locally so I'm not sure if that's what's breaking everything. Maybe my local can't communicate with twitter.
and
Comment #57
chaloum CreditAttribution: chaloum commentedI see this has been in development for 5 years, any idea when this will be in production?
Thanks
Comment #58
oadaeh CreditAttribution: oadaeh at Hook 42 commentedI'm going to re-roll the patches, due to several patches being recently committed, and see if I can address the error @smustgrave reported.
Comment #59
oadaeh CreditAttribution: oadaeh at Hook 42 commentedSorry for the delay on this. Life got in the way. :)
Here is the re-roll for the 7.x-5.x branch.
I could not create an interdiff, I'm guessing due to some difference in the way the two patches were created, but I could not figure out how to get around that without manually creating the file.
I'll work on updating the 7.x-6.x patch next, but it won't be today. Hopefully, I'll get to it sooner than the 7.x-5.x patch.
Comment #60
oadaeh CreditAttribution: oadaeh at Hook 42 commentedHere is the updated patch for the 7.x-6.x branch.
The interdiff is between this one and the one in #54.
@smustgrave see if this one works better for you.
Comment #61
oadaeh CreditAttribution: oadaeh at Hook 42 commented@chaloum it will only ever go into production when the code works. You can help by reviewing and/or testing it and reporting back what you find. Thanks.
Comment #62
pankajlogical CreditAttribution: pankajlogical commented@DamienMcKenna Thankyou for you help. I applied patch #49. It did not work but I find idea and change in code to work for me. Again thank you for suggesting me the right direction.
Comment #63
DamienMcKenna@pankajxenix: I'm glad you were able to get it working. Would you mind sharing details of what you had to change to make it work? Thanks.
Comment #64
pankajlogical CreditAttribution: pankajlogical commented@DamienMcKenna I use my image field in twitter_post_node_insert function . Instant of this line $files = module_invoke_all('twitter_post_node_insert_files', $node); I just load my image field here .Thankyou.
Comment #65
nakedscientist CreditAttribution: nakedscientist commented@oadaeh - I'm on 7x5x branch; I tried your twitter-support_media_upload_endpoint-7x5x-1981472-59.patch - it failed on "3 hunks" - any suggestions?
Comment #66
megthing CreditAttribution: megthing commentedThis patch;
https://www.drupal.org/files/issues/support_media_upload_endpoint-7x6x-1...
applied correctly on the 7.x-6.2 version.
I'm using the following value as the message in Rules Action > "Post a message to Twitter"
[node:body] [node:field_image]
But only the [node:body] shows up in the tweet.
Comment #67
jimmynash CreditAttribution: jimmynash commentedI've tried the patch from #60 against both 7.x-6.2 and 7.x-6.x-dev without any success.
In both cases I get a bunch of offset successes and fuzzes along with some failures.
Here were the results patching against 6x dev:
I tried to patch the files manually but it got really stinky in some places and I couldn't figure out where some things were supposed to be.
#66 reports success on 6.2 but I've not been able to get that to work either.
Comment #68
sumitk CreditAttribution: sumitk commentedI uploaded my manually patched code here: https://github.com/sumitk/drupal-7-twitter
I didn't create a patch, as I modified it further to make the image upload work with Rules action.
Please use this helper module to identify the correct image field while uploading from node form: https://github.com/sumitk/twitter_image_post