Implementing OAuth2 with Rauth for vk.com - python

I'm trying to implement Oauth2 authorization using Rauth for vk.com provider and I have the following issue:
As far as I know there is no way to obtain users email address through vk.com api call, but it sends email address with access_token in json format.
My problem is: I do not know how to obtain it from Rauth's "session" object, there is an access_token field but no way to get an email address.
Here is the code:
def callback(self):
def decode_json(payload):
return json.loads(payload.decode('utf-8'))
if 'code' not in request.args:
return None, None, None
oauth_session = self.service.get_auth_session(
data={'code': request.args['code'],
'grant_type': 'authorization_code',
'redirect_uri': self.get_callback_url()},
decoder=decode_json
)
me = oauth_session.get("some call to vk.api").json()
Thank you for your help!

Let me explain. Actually, email is returned by Vkontakte along with access token in case it is provided.
You don't need to use oauth_session.get since it does make a new request, but it is already done, when you got access token. Try to get an object property with oauth_session.email.
P.S. you can find email using get_raw_access_token.

Related

Custom ID when registering Fitbit

For my system, my users have their own unique ID (participant_id) that I've provided them.
I have a flask server that registers my users with Fitbit.
#app.route('/fitbit_authorize')
def homepage(): #probably need to send participant_id here
return 'Authenticate with fitbit' % FITBIT_AUTHORIZATION_URL
Fitbit sends a post request regarding the successfulness of my participant registration to the following where I get their user access/refresh tokens for oauth:
#app.route('/fitbit_callback')
def fitbit_callback():
error = request.args.get('error', '')
if error:
return "Error: " + error
state = request.args.get('state', '')
code = request.args.get('code')
token = fitbit_access.get_full_token(code)
I was wondering how can I retrieve the authorizer's original ID (participant_id) in the callback. Is there anyway for me to pass additional information in the fitbit authorization process or what would be the best way for me to retrieve their participant_id?
Good question.
Oauth2 allows for you to include state information in the authorization request---You can include the participant_id in the state parameter: https://dev.fitbit.com/build/reference/web-api/oauth2/
state: Provides any state that might be useful to your application
when the user is redirected back to your application. This parameter
will be added to the redirect URI exactly as your application
specifies. Fitbit strongly recommend including an anti-forgery token
in this parameter and confirming its value in the redirect to mitigate
against cross-site request forgery (CSRF).
In addition to providing the participant_id, you should provide an anti-forgery token to help fight CSRF.

Need a Python script for Slack to deactivate a user [duplicate]

I have tried multiple approaches to this. Tried first getting the user without any user id - this returns me just my user, then tried getting user with other id's and it also retrieves data correctly. However, I can't seem to be able to set user attribute 'deleted'. i'm using this python approach.
slack_client.api_call('users.profile.set', deleted=True, user='U36D86MNK')
However I get the error message of:
{u'error': u'invalid_user', u'ok': False}
Maybe someone has already done this? It says in documentation that it's a paid service mentioning this message under a user property:
This argument may only be specified by team admins on paid teams.
But shouldn't it give me a 'paid service' response in that case then?
The users.profile.set apparently does not work for for setting each and every property of a user.
To set the deleted property there is another API method called users.admin.setInactive. Its an undocumented method and it will only work on paid teams.
Note: This requires a legacy token and doesn't work with App tokens - these are only available on paid plans and new legacy tokens can't be created anymore
in python you can do the following:
import requests
def del_slack_user(user_id): # the user_id can be found under get_slack_users()
key = 'TOKEN KEY' #replace token key with your actual token key
payload = {'token': key, 'user': user_id}
response = requests.delete('https://slack.com/api/users.admin.setInactive', params=payload)
print(response.content)
def get_slack_users():
url = 'https://slack.com/api/users.list?token=ACCESSTOKEN&pretty=1'
response = requests.get(url=url)
response_data = response.json() # turns the query into a json object to search through`
You can use Slack's SCIM API to enable and disable a user. Note that, as with the undocumented API endpoint mentioned in other answers this requires a Plus/Enterprise account.

Instagram API using Python

I am currently using Instagram API in the sandbox mode using Python's python-instagram library. Now, I have an application and its associated client id, client secret and access token, and one connected sandbox user.
Earlier today, I was experimenting with the users/search endpoint. First, I directly used their endpoint URL to send a request:
https://api.instagram.com/v1/users/search?q=XXXX&access_token=<my_access_token>
where XXXX is the connected sandbox user of my Instagram application. This is the generated response:
{"meta":{"code":200},"data":[{"username":"XXXX","bio":"Twitter: #XXXX","website":"","profile_picture":"https:a.jpg","full_name":"XXXX XXXX","id":"22222222"}]}
Now, I tried using the python-instagram library to send request to the same endpoint as follows:
from instagram.client import InstagramAPI
access_token = <my_access_token>
api = InstagramAPI(client_secret='aaaa', access_token = access_token[0])
usr = api.user_search('XXXX')
print usr
However, this is the response I get in this case:
[User: XXXX]
Why is it that I get different responses when I try to call the same endpoint using the direct URL and the Python library?
What python-instagram is doing is that it will take the raw JSON response you get when you issue an HTTP request, and map it to python objects.
When you issue a print usr, you are printing a User object that's in a list, so you see a string which is [User: XXXX].
You can find the model they use for the User object here. It actually directly maps the fields from the Json to get attributes.
Try the following code to retrieve a username and id:
my_usr = usr[0]
print 'User id is', my_usr.id, 'and name is ', my_usr.username

Can't get Facebook Email

I'm following this tutorial here and I have the following code with the python library Rauth and Flask:
def callback(self):
if 'code' not in request.args:
return None, None, None
oauth_session = self.service.get_auth_session(
data={'code': request.args['code'],
'grant_type': 'authorization_code',
'redirect_uri': self.get_callback_url()}
)
me = oauth_session.get('me').json()
print me['id'], me['name'], me.get('email')
return ('facebook$' + me['id'], me.get('email').split('#')[0], # Facebook does not provide username, so the email's user is used instead
me.get('email'))
As you can see, I try to print me['id'], me['name'], me.get('email'). The id and name print as planned though the email prints as None causing an issue with the function (as the email is used at the bottom of the function). When testing this with my personal Facebook account, I made sure I provided the email, I checked the app settings and where it says email, there is a marked checkbox stating that I'm sharing an email with this application.
Has Facebook changed the way the email is handled or received or is there an issue with my app or code? Thanks.
Apparently whit the update to version 2.4 of the APIs Facebook removed the email from the list of fields returned.
If your APP uses version 2.4 of the API you should probably exclude the email field from your code.
For reference:
https://developers.facebook.com/ads/blog/post/2015/07/08/marketing-api-v2_4/
Stefano

Im using python (django framework) to gain a request token from google api, but the request token always comes back empty

Here is sample code that I'm working with.
def index(request):
flow = OAuth2WebServerFlow(
client_id='xyz.apps.googleusercontent.com',
client_secret='xyz',
scope='https://www.googleapis.com/auth/plus.me',
user_agent='sample/1.0')
callback = 'http://%s/oauth2callback' % request.META[ 'HTTP_HOST' ]
authorize_url = flow.step1_get_authorize_url(callback)
return HttpResponse(flow)
For some reason 'flow' is always set to " " or empty instead of a request token. I have searched for days on this issue.
Can anyone tell me why I can't get a request token from google using this method?
fyi: I know that I should be redirecting the user to the authorize url, but I want to see if flow is set before I do since Google will provide the authorize url even if a request token wasn't returned.
Before you can use OAuth 2.0, you must register your application using
the Google APIs Console. After you've registered, go to the API Access
tab and copy the "Client ID" and "Client secret" values, which you'll
need later.
http://code.google.com/p/google-api-python-client/wiki/OAuth2#Registering
If this answer actually helps with your problem then I must bid an R.I.P. to S.O.

Categories