I save ids of all users of my bot in a database but how can I get their current username if I know id? I'm using Python 3 and Telepot framework.
Information about a user from user_id can be obtained by the getChat method in telepot.
bot = telepot.Bot(TOKEN)
info = bot.getChat(user_id)
print(info)
you can get the inforamtion of an account by checking the entity.
You can try to get the entity with this method:
entity = client.get_entity(chat_id)
then Print the entity and try to get the name from there with something like name = entity.chat.name
Related
I've got a python flask app whose job is to work with the Twitter V2.0 API. I got to using the Tweepy API in my app because I was having difficulty cold coding the 3 legged auth flow. Anyway, since I got that working, I'm now running into difficulties executing some basic queries, like get_me() and get_user()
This is my code:
client = tweepy.Client(
consumer_key=private.API_KEY,
consumer_secret=private.API_KEY_SECRET,
access_token=access_token,
access_token_secret=access_token_secret)
user = client.get_me(expansions='author_id', user_fields=['username','created_at','location'])
print(user)
return('success')
And this is invariably the error:
tweepy.errors.BadRequest: 400 Bad Request
The expansions query parameter value [author_id] is not one of [pinned_tweet_id]
Per the Twitter docs for this endpoint, this should certainly work...I fail to understand why I the 'pinned_tweet_id' expansion is the particular issue.
I'm left wondering if I'm missing something basic here or if Tweepy is just a POS and I should considering rolling my own queries like I originally intended.
Tweet Author ID
You may have read the Twitter Docs incorrectly as the expansions parameter value has only pinned_tweet_id, and the tweet fields parameter has the author_id value you're looking for. Here is a screenshot for better clarification:
The code would look like:
client = tweepy.Client(
consumer_key=private.API_KEY,
consumer_secret=private.API_KEY_SECRET,
access_token=access_token,
access_token_secret=access_token_secret)
user = client.get_me(tweet_fields=['author_id'], user_fields=[
'username', 'created_at', 'location'])
print(user)
return('success')
User ID
If you're looking for the user id then try omitting tweet_fields and add id in the user_fields also shown in the Twitter Docs.
The code would look like:
client = tweepy.Client(
consumer_key=private.API_KEY,
consumer_secret=private.API_KEY_SECRET,
access_token=access_token,
access_token_secret=access_token_secret)
user = client.get_me(user_fields=['id', 'username', 'created_at', 'location'])
print(user)
return('success')
You can obtain the user id with user.data.id.
The solution is to drop the 'expansions' kwag and leave 'user_fields' as is. I was further confused by the fact that printing the returned user object does not show the requested user_fields as part of the data attribute. You have to explicitly access them through the data attribute, as below.
I need to list the users with each group that this has associated. I'm trying to do this:
client = boto3.client('cognito-idp')
response = client.list_users(
UserPoolId=env_settings.pool_id
)
listUsers = response['Users']
for u in listUsers:
print u
But I am within their properties does not return the group. I'm using the boto3 python client. Thanks in advance.
ListUsers just returns user metadata but does not have group info. See the syntax of response from ListUsers API call here. To get a user's group info, you would need to make AdminListGroupsForUser API call. The corresponding boto3 call can be seen here.
I need to get the current user id.
In Javascript I use this to obtain the id
uid = firebase.auth().currentUser.uid // uid= ABO0Xc2E6KSDodEhenICkXF371x1
how do I get the uid in python
?
This is the code to retrieve the user id without login or signup.
auth.current_user['localId']
user = auth.sign_in_with_email_and_password(email,password)
print(user['localId'])
this will display the current firebase UID on output screen
You didn't mention a library, but all your other questions are using Pyrebase, so skimming over the documentation (which you should definitely be reading)...
# Get a reference to the auth service
auth = firebase.auth()
# Log the user in
user = auth.sign_in_with_email_and_password(email, password)
# Get the user's idToken
token = user['idToken']
Resurfacing an old question, but looking in to the documentation of Pyrebase here, I noticed that when you refresh the token, the userId is (compared to the other auth functions) retreived. So my solution is:
user = auth.sign_in_with_email_and_password("email", "pass")
user = auth.refresh(user['refreshToken'])
print(user['userId'])
EDIT:
I now see there is a 'localId' variable in the user object even before refreshing - as #Abdul indicated.
I'm trying to find a way to get instagram username from the username ID in Python.
What I found so far is this:
https://api.instagram.com/v1/users/[USER-ID]?access_token=[YOUR-ACCESS-TOKEN]
I have a token number, but it only works for users added to sandbox. For other users I receive the following error:
{"meta": {"code": 400, "error_type": "APINotFoundError", "error_message": "this user does not exist"}}
I'm searching a solution for hours with no luck so far.
Is there a way to arrive to the same result without using a token?
Thanks a lot!
In my case I am getting data from tagFeed so I have picture code. If you have picture code of any of user's post then you can get username like this
https://www.instagram.com/p/picCODE/?__a=1
example: https://www.instagram.com/p/BaGce-NlMg7/?__a=1
it will return a JSON with the owner - username
Point your browser to https://i.instagram.com/api/v1/users/USERID/info/ (replace USERID by the user id). It returns a JSON in which the field user.username is that user's username. With curl and jq installed, this shell oneliner will directly return the username:
curl -s https://i.instagram.com/api/v1/users/USERID/info/ | jq -r .user.username
Alternatively, the Instaloader package provides a convenient way to obtain a username given its ID, without using the official Instagram API.
import instaloader
L = instaloader.Instaloader()
profile = instaloader.Profile.from_id(L.context, ID)
print(profile.username)
It works by querying one of the profile's posts (for which only the profile's ID is required), obtaining that post's metadata (instagram.com/p/SHORTCODE) and then returning the owner.username value of that post.
No, unfortunately you can't. You need a token with public_content permissions, and for that, you need to have a live app (not in sandbox).
i have already developed a FB app.now,i am trying to harvest the user data and store it into the database.But, one thing i am not understanding is how to store the data of the logged in user in mongo db using graph api? prior to that how to fetch data in python?
I know this is repetitive but,i am not able to clear my concept of how to use the api in python.
i have tried this:
#!/usr/bin/env python
# encoding: utf-8
import json
import urllib2
import re
def getdata(id):
'''Queries the Facebook API for the specific group ID, and populates the
results dictionary with the Group ID, User Name, and User ID'''
#An access token is now required for quering the group messages.
a_token='access_token=<access token>'
urlquery='https://graph.facebook.com/'+id+'/feed&limit=20?access_token='+ a_token +''
print urlquery
data=json.load(urllib2.urlopen(urlquery))
harvest = []
results = {}
for item in data['data']:
try:
results = {}
results['grpid'] = id
user = item['from']
results['uname'] = user['name']
results['uid'] = user['id']
harvest.append(results)
except:
pass
print len(harvest)
def getgrpids():
urlquery='https://graph.facebook.com/<any username>'#can i put my app name?
#not clear from examples given on facebook graph api page.
data=json.load(urllib2.urlopen(urlquery))
ids=[]
for item in data['data']:
try:
ids.append(item['id'])
except:
pass
return ids
def main():
idres=getgrpids()
for id in idres:
#Loops through all of the group ids returned by getgrpids()
print 'Group ID:', id
getdata(id)
if __name__ == '__main__':
main()
Now the problem goes like this.When i change the username to some other the error report says that the user should be logged in or the app cannot get the user details.I am not understanding this since my friend was online at that time still the error?
Am i missing something?secondly i am not able to put my APP NAME IN THE QUERY (see the comment). Somebody please help.
thanks,
Assuming that you have access token of the user:
import facebook as fb
graph = fb.GraphAPI(access_token)
profile = graph.get_object("me")
# if you want to get the user's profile picture (you need to have the session UID)
profile.update({"picture": "http://graph.facebook.com/%s/picture?type=large" % uid})