Can't generate facebook access token - python

I need to generate the Facebook access token for a project.
Code:
import requests
import json
url = 'https://graph.facebook.com/oauth/access_token'
payload = {
'grant_type': 'client_credentials',
'client_id': 'clientid',
'client_secret': 'secret'
}
response = requests.post(url, json=payload)
print (response.json()['access_token'])
What I'm getting as output is my id|type, after research I found that it is due to the 'client_credential' request. I need to generate access tokens for a project (I want to use them to find likes etc). Any help would be appreciated, it's my first time attempting a project like this.

use [facebook-sdk.][1]
import facebook
app_id = 'YOUR_APP_ID'
app_secret = 'YOUR_APP_SECRET'
graph = facebook.GraphAPI()
# exactly what you're after ;-)
access_token = graph.get_app_access_token(app_id, app_secret)

Related

How to get data from amoCRM through API?

I am trying to get data through API from amoCRM system but I am having some problems and I am really tired.
The url: https://alamiri.amocrm.ru/api/v4/contacts
This API should return contacts of users that contain (first name, email, number, ...).
When I copy and paste the above link in the browser, I get the data and everything works fine but when I try to get that data in Python it returns the following:
{'detail': 'Invalid user name or password',
'status': 401,
'title': 'Unauthorized',
'type': 'https://httpstatus.es/401'
}
my code in python (Django framework):
import requests
import pprint
def get_contacts():
url = "https://alamiri.amocrm.com/api/v4/contacts"
api_key = "def50200c1f82415f4625f96e402d967b880e33938b94d6ad8edd6c305cda8a3fff80990871fba6aac89391440c6330a26768410a52b0e332b2c6a1b2e49cbe912132194d4bb8c4a15bb304c14f649f1149826fc0eeaf1311f23feeea1118cba72af36e76c22f30fa2fb81a4f0678cde451492504ba56eff9eb7af876e49ad721105a9b8bcc94400bf21e9c9f5de43a17daa439d5af64a9ae8e54bf8c7005f4f01515cfe8756a0ddd209fd91c4ca15bc336ef89523876dd39f6472f190f82b1ecd4d9d0cfccc6941c1ffb64d79e2e6dbec1b12331154a7b343ac10abfc05f432d09da878d41a37cdacc50b1f18419594e669854efa108c17384143a71a4d6739bad60cdc704728e2c0dce82ff5707d8d6c8f34ce59aa1855a42153b3703635881fe1cc4a037070846a938a9d124845e1f217fd63c15ed99c79cbee2646b4df8018addaf76289708587a2b182747423e490c3ec21b9fd78c69cd73f2d8985cf916fd50b24e23a1a722d61449a8cd2dea4d1e4d334b103fb311cd1bf5454e0a979c52e65c303baa96d70d655f9a0a300a7ba78c5cc1aa0234d88eefd9bb11e245615257b8955f031f76cec978ed0e3eb3c479b83b91bef4158728d"
login = "my-email-that-I-used-to-create-account-on-amocrm"
client_uuid = "e1899afa-85a8-45ca-93f9-7f4b26374a5d"
client_secret = "jUlw4E6H417MVuzsujIW39GXXjpLjratAwOEiaqPr3KDBKYwc9ZQLXK2UlhMcsuu"
headers = {
'login': login,
'client_uuid': client_uuid,
'client_id': client_uuid,
'client_secret': client_secret,
'grant_type': 'authorization_code',
'Authorization': f'Bearer {api_key}',
'session_id': "a8f1uutp26uipj414pfudab8cv",
'refresh_token': "def50200e673c8cf44d3c3a28df221ffb9d97c2f25cffefa09337953dc1fcc9b9aaa479235f8a6840cde697ecb58252452a22a7a71696665ff19a515db602601743799729b799a6d98d743ba33ab23b1d750ae479a068d16dd9d503062023a8406728c55afb37be5c4cf36df5a3c93bf06c9ff097c54db83d90308a92999669c80506590df2e609fa2aa2d55f3066d06efde55ab0a3c64c44a87743c5db3115632a6ecbd498cbc8012ccddfeabfe7ad9ad0f01664e72c434ccacd686be0e9582567ebdf6abb0cd4258c3b16bbb487222be774086398930ef18f2181a8b6d184a9ce61454710cc3bcaf01e978067a059aaa16c322c8d7b0e5cf34069b0f3ff1e2f5e5a665ec798d6f3d05bb95912528ef8b3230c3e2d03fbd1bbc4087b4bca54d050f876589b7c9fbc1c45199a58b021afa832776ed22637ea1e940dbb3156acec1c0b6320066a2045e26dcd5b6b169fb773ca11ebbb398b513ef8f6f04f973506a8c01c48dad944aadf078db881e8f7de959c187ae7cc41135e8c151bd89e5b8d71c2dc20ee6f282fefd8cb93fcd0f0942ab20ae457da41ea22fed3d881d21585219defc611531ca3909a632095e6c1d568f2408f4eab7b4b8aa90cb892d2253925b8682b9dce4f5",
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImJkZDk0MTA1NTc5NWY0YzAwOTNmZDAwMmIxMDE1NDczZTZiN2ZjZDBlZGJlNWJlMWJjMjYzYTY2YTY2MGRlODA0OWQ5MjM5ZTE4ODY2MmQyIn0.eyJhdWQiOiI3YjQ5MGYxOC0yYmU1LTRhNTUtYTc0ZS0xOTYzYzk2NDFiOTgiLCJqdGkiOiJiZGQ5NDEwNTU3OTVmNGMwMDkzZmQwMDJiMTAxNTQ3M2U2YjdmY2QwZWRiZTViZTFiYzI2M2E2NmE2NjBkZTgwNDlkOTIzOWUxODg2NjJkMiIsImlhdCI6MTYzNzMzOTYzMywibmJmIjoxNjM3MzM5NjMzLCJleHAiOjE2MzczNDIwMzMsInN1YiI6Ijc2NDM4ODQiLCJhY2NvdW50X2lkIjpudWxsLCJzY29wZXMiOlsiY2hhdHMiLCJjcm0iLCJub3RpZmljYXRpb25zIiwidW5zb3J0ZWQiLCJtYWlsIl19.VEz7bIXpbsL-iMCz51UgW6JU1rySQTIXBLf_xPdBcmMpR3yIL8raUgCjEBJ05E05lhMsYxxwKEYg2yN468jkqfz5wTfmRdGs9ECiVatZ6oMEJYFUn7AF5JaP0fQw18raZdAo8CQJfZS86IuHEYpU2k-lW5LmDC2dIIyZYe4irnln0KRw5_mgci4JNPEyeqzYADI0deqX-rraXFjFm6UP4KkByfOrgfopyOV8cIBA_34i4CWewbVQLJF4-HWGuKvaOH5Erjh-GUNViyCCuH2Q4F7dI6gwhAd2z2lEbbTR_x4oyRx04QA1YOZjI64SJQAaaIYDFLphr7D4V59w44zG1g",
"csrf_token": "def50200e00c062021bac6b78d51f10f45cc28add00268d9",
}
response = requests.get(url, headers=headers)
pprint.pprint(response.json())
get_contacts()
I am reading their documentation (amoCRM) and I tried a lot of things but none worked! 😥
https://www.amocrm.com/developers/content/platform/abilities/
I followed their instructions how to work with their API:
https://nova-amocrm.notion.site/894277ffef5940e7a5daadd2abc746c8
But I am stuck and I don't know how to solve this problem!
Any help please?
If you don't know what is amoCRM, then
Firstly, you should create an integration in your amoCRM account and use your google colab notebook link to redirection (or any other link).
enter image description here
Next, use the request to get refresh code and access token:
data = {'client_id' : '3d31ef87-...',
'client_secret' : 'xyEFPbRH5xw1...',
'grant_type' : 'authorization_code',
'code' : 'def50200ec88c8fe724cfc58ebd7e19...',
'redirect_uri' : 'https://colab.research.google.com/drive/...'}
new_url = 'https://your_domain/oauth2/access_token'
request = requests.post(new_url, data=data)
request_dict = json.loads(request.text)
refresh_code = request_dict['refresh_token']
access_token = request_dict['access_token']
Next, define the request headers:
api_call_headers = {'Authorization': 'Bearer ' + access_token}
And after that you can get any information using filters:
api_statuses_response = requests.get('https://alamiri.amocrm.ru/api/v4/contacts?filter[id]={id}&filter[created_at][from]={from}&filter[created_at][to]={to}', headers=api_call_headers, verify=True)
print(api_statuses_response.json())

Response 400 'Bad Request' when trying to get access token from Reddit API

I'm trying to use the reddit API to pull text data for a machine learning project, but I can't get the OAuth access token. Instead of responding with the access token, the response just says bad request.
Here's my code:
import requests
auth = requests.auth.HTTPBasicAuth('*****', '****')
data = {'grant_type':'password',
'username' : '*****',
'password':'******'}
headers = {'User Agent': 'win64 : **** v1.0.0 by /u/*****'}
res = requests.post('https://www.reddit.com/api/v1/access_token', auth=auth, data=data, headers=headers)
print(res)
I've triple checked the personal use script and secret, and so I'm struggling to think of what's wrong. Any ideas?

Pexels API Python Request

I tried doing API request with Python for Pexels, but get authentication error. Documentation link Here is my code:
import requests
video_base_url = 'https://api.pexels.com/v1/search'
api_key = 'my_key'
my_obj = {'Authorization':api_key, 'query':'Stock market'}
x = requests.get(video_base_url,data = my_obj)
print(x.text)
But I get "error": "Authorization field missing". Any help is appreciated.
You are sending the authorization in the body of the request
You need to send it in the header
r = requests.get(video_base_url, headers = {'Authorization': api_key}, data = my_obj)

Posting to Wordpress Rest API fails to work in Python

Cannot seem to find nor create a minimum working example of posting an article to the REST API. I have the following set up and code.
import requests
import json
import base64
user = 'NASA'
password = 'bah bah ...'
url = 'http://any.com/wp-json/wp/v2'
data_sauth = user + ':' + password
post = {
'title': 'Rubbish',
'slug': 'rest-api-1',
'status': 'publish',
'content': 'random stuff',
'excerpt': 'lala',
'format': 'standard'
}
token = base64.standard_b64encode(data_sauth.encode())
headers = {'Authorization': 'Basic ' + token.decode('utf-8'), 'Content-Type': 'application/json'}
r = requests.post(url + '/posts', headers=headers, json=post)
print(r.status_code)
print(json.loads(r.content))
Now I get a 200 code. But I just get no post appearing and return of the posts. I cannot find where this might be going wrong. The API reference is here: https://developer.wordpress.org/rest-api/reference/posts/#create-a-post
On the WP side, I have installed the plugin named Application Passwords and ensured that the credentials match. No post appears, I get a 200 status code, and get a poll from the WP site of the existing posts, but no new post.
I know this is super old but I tried your code with my credentials and it worked perfectly, check that you're requesting to the right URL (some sites have different URLs for the API).

How To Authenticate HumbleBundle

I want to write a program to automatically download my Humble Bundle purchases, but I'm struggling to login to the site. I thought that it should be a pretty straightforward process:
import requests
LOGIN_URL = "https://www.humblebundle.com/processlogin"
data = {
"username": "username",
"password": "top_secret",
}
session = requests.Session()
session.params.update({"ajax": "true"})
response = session.post(LOGIN_URL, data=data)
json = response.json()
print(json)
But I get a rather unhelpful failure message
{'errors': {'_all': ['Invalid request.']}, 'success': False}
What am I doing wrong?
I don't think that its going to let you do that. If I had to guess you're going to have to use OAuth.
Humble Bundle uses a CAPTCHA to ensure only humans are logged in. Only logged in users seem to be able to retrieve information about their purchases (I have not found another way to authenticate myself).
By design, a CAPTCHA disallows scripts to log in. My best suggestion is to log in with a regular webbrowser, and store the value for the cookie called '_simpleauth_sess' locally. You can use that to retrieve data as if you're logged in.
Here is an example with requests library that the OP uses:
cookies = dict(_simpleauth_sess='easAFa9afas.......32|32u8')
url = 'https://www.humblebundle.com/api/v1/user/order'
r = requests.get(url, cookies=cookies)
print(r.text)
Or a bit more complex:
session = requests.Session()
session.cookies.set('_simpleauth_sess', 'easAFa9afas.......32|32u8',
domain='humblebundle.com', path='/')
r = session.get('https://www.humblebundle.com/api/v1/user/order')
for order_id in [v['gamekey'] for v in r.json()]:
url = 'https://www.humblebundle.com/api/v1/order/{}?wallet_data=true&all_tpkds=true'.format(order_id)
r = session.get(url)
...

Categories