Twitter - Upload Image Using API and Get URL - twitter

I am creating a twitter share button that a user can click but I also want to attach an image to the tweet.
I am uploading the image to Twitter using the API so have the media ID available.
Is there a way to get the URL from this ID so I can attatch it to a Twitter Intent link?
Or can I only use this Media ID with the API to post a tweet for an authenticated user?
I am trying to attatch a Twitter hosted Image to a Share button so that anybody can post it to their Twitter feed

Before you can attach the image to a tweet, you would need to upload the image to twitter first. I think you are already doing this using the endpoint media/upload
Reference:
https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload
Once this is done issue another POST request to the endpoint statuses/update with parameters 'status' and 'media_ids'. Twitter expects a comma separated list of media_ids. You already have this in the object returned by media/upload endpoint.
Reference:
https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update
The object returned in the above call has the url for each media uploaded under the property entities.
This is an excerpt from how I did using PHP and abrahamoauth library. $connection is the twitter connection object.
$media = $connection->upload('media/upload', array('media' => $_FILES["tweet_image_file"]["tmp_name"]));
$parameters = array(
'status' => '',
'media_ids' => implode(',', array($media->media_id_string)),
);
$result = $connection->post('statuses/update', $parameters);
//$image_url = $result->entities->media[0]->media_url; //stopped working from July 2015
$image_url = $result->entities->media[0]->url;

Related

Direct link to Twitter retweet

I am working on a Twitter app and I want to find a way to link directly to a retweet.
For example. If user A makes a tweet, and user B retweets it, I want a URL that will take us to user B's profile where the retweet is displayed. What is that unique URL?
I recently needed to get a link to an arbitrary retweet that was too old for the search feature to work.
Here's what I did:
Open the Network Monitor
Visit the timeline of the retweeter
Scroll until you find the retweet
Look at the Network Monitor, open the latest request to the endpoint /UserTweets
Either
go to the "Response" tab and sift through its JSON manually to get the id_str to craft the URL:
or, paste the following into the Console, then right-click on the response, “Copy…Response”, and return to the console to press Enter:
UserTweets = JSON.parse(prompt("Paste the copied Response data:"));
// get the output from that endpoint,
UserTweets
// dig through the packaging,
['data']['user']['result']['timeline_v2']['timeline']['instructions']
// filter out "pinned tweets" metadata,
.filter(cmd => cmd['type'] == "TimelineAddEntries")
// de-batch,
.flatmap(cmd => cmd['entries'])
// filter out "cursor" metadata,
.filter(entry => entry['content']['entryType'] == "TimelineTimelineItem" )
// then dig through a bunch more layers of packaging,
.map(entry => entry['content']['itemContent']['tweet_results']['result'])
// munge the tweets into a more-usable format,
.map(tweet => ({user: tweet['core']['user_results']['legacy'], tweet: tweet['legacy']}))
// filter out non-retweets,
.filter(({tweet}) => tweet['retweeted_status_result'])
// extract just the RT text and URL of the retweet itself,
.map(({user, tweet}) => ({url: `https://twitter.com/${encodeURIComponent(user['screen_name'])}/status/${encodeURIComponent(tweet['id_str'])}`, text: tweet['full_text']}))
// print results.
.forEach(({url, text}) => console.log(url, text))
…et voilà:
https://twitter.com/ThePSF/status/1398372497741946884
Internally, all Retweets are simply special Tweets. This means each Retweet also has an ID (which is stored on id and id_str at the root of the Tweet object). For proof, here's a Retweet from the Twitter Retweets account.
If you're using Tweet Streams (e.g. statuses/filter), you'll be able to pick up this ID from the returned Tweet object of the retweet. From the ID, you can just build a normal Twitter link with https://twitter.com/<username>/status/<retweet id>. Assuming your Retweet object is named retweet, the correct link would be (in JavaScript) `https://twitter.com/${retweet.user.screen_name}/status/${retweet.id_str}`.
If the Retweet is fairly recent, you can do a status search (search/tweets) on the user's profile (i.e. you have to set the q parameter to from:<username>) to find the Retweet. You'll most likely have to cross check the ID of the Tweet you want and the ID of the Retweet you're looking for to be sure, though.
If you're trying to get the Retweet ID of an old Tweet, however, you might have to use Twitter's Premium APIs, which are paid.
JamesTheAwesomeDude's answer worked for me with some light modifications for the new twitter API:
UserTweets = { /*PASTE HERE*/ }
// .timeline changed to .timeline_v2
UserTweets.data.user.result.timeline_v2.timeline.instructions
.filter(cmd => cmd['type'] == "TimelineAddEntries")
.map(cmd => cmd['entries']).flat()
.filter(entry => entry['content']['entryType'] == "TimelineTimelineItem" )
.map(entry => entry['content']['itemContent']['tweet_results']['result'])
// .user changed to .user_results.result
.map(tweet => [tweet['core']['user_results']['result']['legacy'], tweet['legacy']])
.filter(([user, tweet]) => tweet['retweeted_status_result'])
.map(([user, tweet]) => [`https://twitter.com/${user['screen_name']}/status/${tweet['id_str']}`, tweet['full_text']])
.forEach(([url, rt_text]) => console.log(url, rt_text))

Youtube api channel selector

Now you can create more than one Youtube channel. In my Android app I need to let user select one of these channels and work with it (get subscription videos, etc..).
I'd like to emulate the official Youtube app where you can choose one of my channels that I have created in my Youtube account.
EDIT:
Finally I solved the problem with the Ibrahim suggestion. In a webview I used the oAuth2 authentication like this:
https://accounts.google.com/o/oauth2/auth?client_id=CLIENT_ID&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/youtube&response_type=code&access_type=offline
Then receive the token to use to get the access_token:
String url = "https://accounts.google.com/o/oauth2/token";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("grant_type", "authorization_code"));
nameValuePairs.add(new BasicNameValuePair("client_id", YoutubeBase.CLIENT_ID_NUMBER));
nameValuePairs.add(new BasicNameValuePair("client_secret", YoutubeBase.CLIENT_SECRET));
nameValuePairs.add(new BasicNameValuePair("code", code));
nameValuePairs.add(new BasicNameValuePair("redirect_uri", YoutubeBase.REDIRECT_URI));
response = RestClient.postGoogleData(url, nameValuePairs);
And finally use the access_token to call to Youtube Data API:
https://gdata.youtube.com/feeds/api/users/default?v2.1&access_token=" + accesToken
I've used with API v2 but works with v3.
There is no Page selector built-in with the current Android native OAuth2 client. Till that is built, the only workaround is that, doing the web version Oauth2, get the token, and use that token in your requests later on.

Factual crosswalk api, can I get a place's foursquare venue id with its corresponding facebook page id in 1 request?

I have a database full of places with the facebook page id's. I'd like to map them to the corresponding foursquare venue id with the factual api.
The problem is when I try to issue a request and set the namespace id to the facebook id all I get back is the entry that factual has with the factual_id and facebook id but not any other third party places with the same factual id.
What I'm doing now is saving the factual id, and then sending another request given the factual_id which returns all the crosswalk entries then I can look for the foursquare one. Is there anyway to do this request in 1 step?
Using php here is the code.
The place variable is a object that is basically mapped to a facebook page
$place = Model_Place::find(42);
$factual = new \Factual(\Config::get('factual_oauth_key'), \Config::get('factual_oauth_secret'));
$query = new \FactualQuery;
$query->limit(1);
$query->field("namespace")->equal("facebook");
$query->field("namespace_id")->equal($place->page_id);
$res = $factual->fetch("crosswalk", $query);
$data = $res->getData();
echo "<pre>";
print_r($data);
echo "</pre>";

Youtube Video Upload in User Wall

I used the Facebook PHP SDK to upload a YouTube Video in the FB user wall.
I used the "source" option in "/USER_ID/feed/" of Graph API.
USER_ID is the logged in user's Facebook ID.
My code was working fine.
But Facebook made some changes in their API and the code is not working anymore.
Only the Youtube video image is showing but the Youtube video is not playing in facebook.
My code looks like this:-
$params = array(
'access_token' => $fbToken,
'message' => $name.' has shared a Vhybe',
'link' => $link,
'name' => 'Vhybe Social',
'caption' => $title,
'description' => $content
);
$sourceUrl = "https://www.youtube.com/v/".$videoId;
$imageUrl = "http://i4.ytimg.com/vi/".$videoId."/default.jpg";
$params['source'] = $sourceUrl;
$params['picture'] = $imageUrl;
$result = $facebook->api(
'/'.$userId.'/feed/',
'POST',
$params
);
I tried the "Graph API explorer" tool from the Facebook developer tools section
URL => https://developers.facebook.com/tools/
But i am getting the same results.
If the above process to upload Youtube video to user wall has been deprecated can you please suggest me an alternate process.
Thanks in advance.
Sincerely,
Sourav Mukherjee
Well the above code is working fine again.
I did not make any changes in my code.
Looks like Facebook corrected their own issue.
Sincerely,
Sourav Mukherjee

Which is the right Youtube Api URL to rate a video with an authenticated user?

I'm doing a Android App and I've done that an user can authenticate with OAuth 2.0 in a Youtube account. But when I try to rate a video which is the right URL to rate it.
I send a POST request with this string:
https://gdata.youtube.com/feeds/api/videos/sxbHyiwYDiw/ratings?value=like&key=my_access_token&v=2
https://gdata.youtube.com/feeds/api/videos/video_id/ratings?value=like_or_dislike&key=my_access_token&v=2
It return this response: "Response contains no content type"
Edit:
I use this link to check it: http://gdata.youtube.com/demo/index.html
I can do this action sending this POST URL: "https://gdata.youtube.com/feeds/api/videos/sxbHyiwYDiw/ratings"
Adding this headers
httppost.setHeader("POST", "/feeds/api/videos/sxbHyiwYDiw/ratings");
httppost.setHeader("GData-Version", "2");
httppost.setHeader("X-GData-Key", "key=*********");
httppost.setHeader("Authorization", "AuthSub token=" + getAccessToken());
httppost.setHeader("Content-Type", "application/atom+xml");
...and adding a xml body
You can check it in the first link: Youtube Demo.

Resources