Retrieve screen name from twitter account? - ruby-on-rails

Is it possible to get a twitter account's username from an instance of the Twitter REST client? Specifically, I would like to get the name for the twitter account associated with the client's access tokens and secrets.
I searched through the twitter gem documentation on rubydoc and looked at the Twitter API user object documentation but wasn't able to solve the problem. I did try using client.attributes, client.to_h and client.screen_name, but received an unknown method error.
For context, I'm currently working on a twitter bot that auto-replies to hashtags when it is looped into a conversation. We want to prevent the bot from tweeting at itself, so we are currently hard-coding in the bot name as an account not to tweet at. It would be helpful if we could replace the hard-coded name with something like client.account_name.
Thanks for reading.

Taking a look at the documentation for the client object of the twitter gem says it has a user method (Methods included from Users section) [1]. The documentation for the user method states it returns a Twitter::User object of the currently authenticated user [2]. This class inherits from BasicUser which is where the screen_name method exists [3].
client.user.screen_name
http://www.rubydoc.info/gems/twitter/Twitter/REST/Client
http://www.rubydoc.info/gems/twitter/Twitter/REST/Users#user-instance_method
http://www.rubydoc.info/gems/twitter/Twitter/BasicUser

You say
I would like to get the name for the twitter account associated with the client's access tokens and secrets
and
We want to prevent the bot from tweeting at itself, so we are currently hard-coding in the bot name as an account not to tweet at.
If I understand you correctly, you want to find out which application was used to send a tweet - so you can filter it out?
In each tweet, you will find an attribute called source. This shows which app the user sent the tweet with.
For example,
"created_at": "Sun Mar 11 11:25:05 +0000 2018",
"source": "\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c\/a\u003e",
"in_reply_to_status_id_str": "972414342166654976",
That Tweet was sent using Twitter for Android's API keys.
And in this example:
"source": "\u003ca href=\"http:\/\/shkspr.mobi\/blog\/tag\/solar\/\" rel=\"nofollow\"\u003eEdent's Solar Panels\u003c\/a\u003e",
"in_reply_to_status_id": null,
The Tweet was sent from my personal API key.
Give your API key a unique name and URL. When you retrieve Tweets, check the source parameter, and don't reply to any which have been sent using your access token and secret.

Related

How to link slack user with github account?

I am creating a slack bot and want to be able to link the slack user with the github account.
Is there a way I can link the 2 either via slack or github API's?
I was thinking if of storing users slack username and github username in a JSON object, i.e.:
{
"slack_username": "JoeBlogs",
"github_username": "JoeBlogs123"
}
In order to do this, I would need to retrieve the users' username from slack API when the user authorises the app / bot.
I was thinking if I add a redirect_url to my slack app then it would redirect user to http://example.com/redirect if the users data is sent along to this redirect url, I would be able save it in a database of sorts.
If I then did the same with the github API then I could reference database in order to find slack users JoeBlogs github account and vice versa.
Is it possible to use the redirect_url like this? I couldn't see any user data being sent to but maybe it is nested somewhere I couldn't see it?
Is there a better way to link the 2 accounts?
Assuming you are using Install button to install your Slack app into the workspace, this is a payload which will be sent to your response_url:
{
"access_token": "xoxp-XXXXXXXX-XXXXXXXX-XXXXX",
"scope": "incoming-webhook,commands,bot",
"team_name": "Team Installing Your Hook",
"team_id": "XXXXXXXXXX",
"incoming_webhook": {
"url": "https://hooks.slack.com/TXXXXX/BXXXXX/XXXXXXXXXX",
"channel": "#channel-it-will-post-to",
"configuration_url": "https://teamname.slack.com/services/BXXXXX"
},
"bot":{
"bot_user_id":"UTTTTTTTTTTR",
"bot_access_token":"xoxb-XXXXXXXXXXXX-TTTTTTTTTTTTTT"
}
}
See for details.
Then, using access_token in combination with users.identity API method you will get basic information about Slack user (playing with the scopes you requested during the install process you can get different fields of Slack user identity).

GET user endpoint always returns MY user data, regardless of ID in query

I am querying the Microsoft Graph API to get OneDrive files, like:
GET https://graph.microsoft.com/v1.0/me/drive/root/delta
There is a lastModifiedBy facet in the driveItem resource returned, but it only contains a displayName and id. I need the user's email based on that ID.
So I tried the endpoint from these docs, which is:
GET https://graph.microsoft.com/v1.0/users/{id}
Specifically, I have tried things like:
GET https://graph.microsoft.com/v1.0/users/45aa3379f269b493
But it always returns the exact same set of data (regardless of which id I pass in), which is the data from the https://graph.microsoft.com/v1.0/me endpoint.
My scopes are the following:
offline_access
files.read.all
user.read
user.readbasic.all
people.read
contacts.read
contacts.read.shared
Is there any reason that this endpoint is not returning the data for the specific user ID that I pass? Or is there an alternative way to get the user's email based on the information provided in lastModifiedBy?
Looking at theJSON you posted, I noticed your files have an id using the format xxxxxxxxxxxxxxxx!xxx (note the !). This tells me this is a personal Microsoft Account (MSA).
Since this is a Microsoft Account, there are no "users" beyond yourself. Graph will therefore disregard whatever id you provide and always return your own profile (/me). In order to use /users you need to be connected to an Azure Active Directory tenant.
You cannot pull profile information for other MSA/Outlook.com accounts. Were this supported it would introduce an unacceptable hole in MSA privacy/security.

weibo get status of other user?

I want to get the timeline post of the other user using weibo api but not getting.weibo API. I am using few API Call like user_timeline,trends but not getting any proper data. please, let me know if I missing any API or any settings in App. I have appKey, secret key and redirect url.
As far as I can tell, the API calls that would get you this info have all been restricted. The English documentation is not updated, but the chinese documentation specifies for example that you can only use User_timeline to get your own timeline, not others.
http://open.weibo.com/wiki/2/statuses/user_timeline
接口升级后:uid与screen_name只能为当前授权用户

"Via MyApp" tag using Twitter4J in a Java Desktop Application

Well, the title explains my question. I'm developing an app that will be something like a Twitter client, and I can't find a way to make that when I publish a tweet in my account using the app the tweet shows "via MyApp". I'm using another Twitter account, not the one that I used to create the app (I don't know if that has something to be with all this, but could be so I'm telling you). Does anyone know if this is posible?
Thanks!
According to the field guide, your JSON object (named obj) has a field obj["source"] with the following meaning:
Utility used to post the Tweet, as an HTML-formatted string. Tweets from the Twitter website have a source value of web.
This means that "via MyApp" value is the client used to post the tweet. You can find the user that posted this tweet in the field obj["user"]["screen_name"]
References:
https://dev.twitter.com/overview/api/tweets
https://dev.twitter.com/overview/api/users

Google auth2 API has missing timezone info?

recently I've started using http://code.google.com/p/google-api-php-client/ (Google OAuth2) for my web app.
However I draw a blank because I cannot read the timezone for my user.
I tried enter it somehow on my Google account page but I didn't found any such field.
Does anybody know if Google really offer such information and if so how can I set it to my account for example?
The closest thing available to you is the timezone available on your Google calendar. Check the 'General' tab on your calendar settings page: https://www.google.com/calendar
Now, you can request access to the user's calendar while you're authenticating them to your service. Use the scope "https://www.googleapis.com/auth/calendar" (docs)
When you're authenticated, you can request the calendar you're interested in. I guess the user's primary calendar is your best bet.
Finally, each calendar has settings with a timeZone property.
Here's sample code for the Google API PHP client.
As of March 21, 2013 Google is not providing this information in their response to the userinfo.profile endpoint -- even though they claim that they do.
This documentation claims that timezone is available:
https://developers.google.com/accounts/docs/OAuth2Login?hl=en#userinfocall
However, the current implementation only returns the following data:
id
email
verified_email
name
given_name
family_name
link (to G+ profile, if available)
gender
birthday
locale
hd (appears to be the Google Apps domain, if relevant)
Note that "birthday" and "hd" are not listed in the current documentation on the page listed above. I am verifying these fields from real-world responses I'm receiving from the endpoint.
The best approach for getting timezone at the moment is probably to use JavaScript (to retrieve the timezone from the user's browser). This StackOverflow answer explains how to do so:
Getting the client's timezone in JavaScript
Unfortunately, this would have to occur as a secondary action, meaning you'd have to add this data to your database separately from the data retrieved from the endpoint. One trick to consider is capturing the timezone offset on the page where you initiate the endpoint request, and then appending the timezone offset value to the "state" value in your OAuth request. This value will then be returned on the callback, and you could extract and add it to the other profile information that was returned during the same procedure in which you write to your database.

Resources