Unable to post to bitbucket using OAuth2Session and requests_oauthlib in python - python-2.7

I have a python script that auto refreshes and gets a new access token to access bitbucket end points.
My script works as long as it a 'GET' request. I am trying to do a post to create a private repository inside a private project. But I am getting a 400 status code which means my request is malformed.
For eg:
protected_url='https://bitbucket.org/api/2.0/repositories/<team>'
from requests_oauthlib import OAuth2Session
client = OAuth2Session(client_id, token=token, auto_refresh_url=refresh_url,auto_refresh_kwargs=extra, token_updater=token_saver)
token=client.refresh_token(refresh_url, **extra)
token_saver(token)
client = OAuth2Session(client_id, token=token)
r = client.get(protected_url)
print r.content
works and fetches the list of repositories in bitbucket but
data1={"scm": "git", "is_private": "true", "fork_policy": "no_public_forks", "project": {"key": "project_key"}}
data=json.dumps(data1)
headers = {'Accept': 'application/json'}
protected_url='https://bitbucket.org/api/2.0/repositories/<team>/<new_repo>'
from requests_oauthlib import OAuth2Session
client = OAuth2Session(client_id, token=token, auto_refresh_url=refresh_url,auto_refresh_kwargs=extra, token_updater=token_saver)
token=client.refresh_token(refresh_url, **extra)
token_saver(token)
client = OAuth2Session(client_id, token=token)
r=client.post(protected_url, data=data, headers=headers)
print r.content
gives a 400 status code(the request is malformed) with the error
'{"type": "error", "error": {"message": "Private projects cannot
contain public repositories"}}'
. I have the access token to authenticate, my URIs are correct, it is just the syntax of the post request that looks incorrect. Can anyone help me fix this?

Related

Getting 400 Bad Request from Open AI API using Python Flask

I want to get response using Flask from OpenAI API. Whether I am getting Status 400 Bad Request from Browser through http://127.0.0.1:5000/chat
Bad Request
The browser (or proxy) sent a request that this server could not understand.
Also I am checking this from Postman
from flask import Flask, request, render_template
import requests
app = Flask(__name__)
#app.route('/')
def index():
return 'Welcome to ChatGPT app!'
#app.route('/chat', methods=['GET', 'POST'])
def chat():
user_input = request.form['text']
# Use OpenAI's API to generate a response from ChatGPT
response = generate_response_from_chatgpt(user_input)
return response
def generate_response_from_chatgpt(user_input):
api_key = "YOUR_API_KEY"
url = "https://api.openai.com/v1/engines/davinci/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"prompt": user_input,
"engine": "davinci"
}
response = requests.post(url, headers=headers, json=data)
return response.json()["choices"][0]["text"]
if __name__ == '__main__':
app.run()
It would be best if you check the openai documentation to make sure you are using the correct endpoint and data format in your request.
Also, you should check your API key, if it is correct and if you have reached the limit of requests.
Also, it's worth noting that the code you provided is missing the import statement for Flask. You will need to add the following line at the top of your file:
from flask import Flask, request
Also, I see that you're using request.form['text'] but you should check if the request is a GET or POST request.
if request.method == 'POST':
user_input = request.form['text']
else:
user_input = request.args.get('text')
This is to avoid a KeyError being raised when the request is a GET request.

Using telethon with a django application

I want to watch updates on telegram messages in an django application and interact with django orm.
I found telethon library, it works with user api which is what I want.
Below code simply works on its own.
from telethon import TelegramClient
from telethon import events
api_id = 231232131
api_hash = '32131232312312312edwq'
client = TelegramClient('anon', api_id, api_hash)
#client.on(events.NewMessage)
async def my_event_handler(event):
if 'hello' in event.raw_text:
await event.reply('hi!')
client.start()
But telethon requires phone message verification and it needs to work in a seperate thread.
I couldn't find a way to put this code in a django application. And when django starts, I dont know how to bypass phone verification.
It should always work in an seperate loop and interact with django orm. Which is very confusing for me.
You can simply use django-telethon and use the API endpoints for signing bot and user session.
run the following command to start the server:
python manage.py runserver
run the following command to start telegram client:
python manage.py runtelegram
go to admin panel and telegram app section. create a new app. get data from the your Telegram account.
request code from telegram:
import requests
import json
url = "127.0.0.1:8000/telegram/send-code-request/"
payload = json.dumps({
"phone_number": "+12345678901",
"client_session_name": "name of the client session"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
send this request for sign in:
import requests
import json
url = "127.0.0.1:8000/telegram/login-user-request/"
payload = json.dumps({
"phone_number": "+12345678901",
"client_session_name": "name of the client session",
"code": "1234",
"password": "1234"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
It's not the answer I wanted initially.
But, I think this is better approach.
Instead of trying to put all of this in django application. It's better to run it seperately and let django application communicate this with rest framework.

How to call an external API webservice from Python in Amazon Lambda?

I am pretty new to AWS Lambda i have a python code which has a post method for making an external API call,
import requests
import json
url = "http://sample/project"
headers = {
'content-type': "application/json"
}
r = request.post(url,headers=headers)
I tried putting it into a AWS Lamda call i tried like this below but it didn't get worked out
import requests
import json
url = "http://sample/project"
headers = {
'content-type': "application/json"
}
def lambda_handler(event, context):
response = requests.request("POST", url, headers=headers)
return response
But i am not getting in any response if i am running from local machine i am getting the output.Please help me how can i make a post call from AWS Lamda

CSRF token failing on second post Django Rest Framework

I'm having an issue where I can log in successfully but any subsequent requests show as
"detail":"CSRF Failed: CSRF token missing or incorrect."
I have no clue what I'm doing wrong, I've looked over Requests docs, DRF docs, turned off authentication to validate the url and searched old SO posts on the subject.
Here is a basic function with attached basic info
def will_fail():
CURRENT_URL = 'http://127.0.0.1:8000/{}'
session = requests.Session()
response = session.get(CURRENT_URL.format('api-auth/login/'))
csrftoken = response.cookies['csrftoken']
first_response = session.post(CURRENT_URL.format('api-auth/login/'),
data={'username': 'itsme', 'password': 'password'},
headers={'X-CSRFToken': csrftoken})
response = session.post(CURRENT_URL.format('api-v1/languages/'),
params={'name': "French", "audio_base": "adpifajsdpfijsdp"},
headers={'X-CSRFToken': csrftoken})
first_response (login):
URL - 'http://127.0.0.1:8000/api-v1/'
Text - {"languages":"http://127.0.0.1:8000/api-v1/languages/","phrases":"http://127.0.0.1:8000/api-v1/phrases/","stats":"http://127.0.0.1:8000/api-v1/stats/"}
Status - <Response [200]>
response (add language):
URL - 'http://127.0.0.1:8000/api-v1/languages/?audio_base=adpifajsdpfijsdp&name=French'
Text - {"detail":"CSRF Failed: CSRF token missing or incorrect."}
Status - <Response [403]>
The settings are very basic since I'd just started on this:
THIRD_PARTY_APP = [
'rest_framework.authtoken',
'rest_framework',
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
Here is the URL info which is also pretty generic
from training.views import LanguageViewSet, PhraseViewSet, PhraseStatsViewSet
router = DefaultRouter()
router.register(r'languages', LanguageViewSet)
router.register(r'phrases', PhraseViewSet)
router.register(r'stats', PhraseStatsViewSet)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^api-v1/', include(router.urls, namespace='api'))
]
I'm using ModelSerializers and ModelViewSets, I didn't override any methods and included all fields.
EDIT:
I had already tried updating the token as well but it gave me a KeyError -
Traceback (most recent call last):
File "C:/Users/Me/PycharmProjects/proj/post_data.py", line 70, in <module>
will_fail()
File "C:/Users/Me/PycharmProjects/proj/post_data.py", line 62, in will_fail
csrftoken = first_response.cookies['csrftoken']
File "C:\Users\Me\Envs\proj\lib\site-packages\requests\cookies.py", line 329, in __getitem__
return self._find_no_duplicates(name)
File "C:\Users\Me\Envs\proj\lib\site-packages\requests\cookies.py", line 400, in _find_no_duplicates
raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
KeyError: "name='csrftoken', domain=None, path=None"
The problem is that your first request logs the user in. Django rotates the token when you log in:
Why might a user encounter a CSRF validation failure after logging in?
For security reasons, CSRF tokens are rotated each time a user logs in. Any page with a form generated before a login will have an old, invalid CSRF token and need to be reloaded. This might happen if a user uses the back button after a login or if they log in in a different browser tab.
The token that you use in the next requests is the token that was used before it was rotated. You need to get the new token from the cookie after the login request.
On top of that, requests transparently follows redirects, and return the last response. Since the second response (probably) doesn't use the token, it isn't set as a cookie. You can use allow_redirects=False to get the first request, and then get the new token from that request. Alternatively you can sent a new GET request for a page that uses the token in the response body, then the token will also be sent as a cookie.
...
first_response = session.post(CURRENT_URL.format('api-auth/login/'),
data={'username': 'itsme', 'password': 'password'},
headers={'X-CSRFToken': csrftoken},
allow_redirects=False)
# Get the new token
newcsrftoken = first_response.cookies['csrftoken']
response = session.post(CURRENT_URL.format('api-v1/languages/'),
params={'name': "French", "audio_base": "adpifajsdpfijsdp"},
headers={'X-CSRFToken': newcsrftoken})
You're likely not using sessions with Requests which would keep the initial response cookies for the next requests.
DRF tells you more about how CSRF works with DRF at http://www.django-rest-framework.org/topics/ajax-csrf-cors/#csrf-protection

i have been trying to parse a website and when using urllib2.urlopen i have been getting a error

can anyone explain me to login to this link(ftpservice.acesphere.com) through python
The URL you are trying to access requires NTLM authentication. You can try python-ntlm package:
from ntlm import HTTPNtlmAuthHandler
import urllib2
url = "http://ftpservice.acesphere.com/stocks/indices/master/indicesmaster_new.ace"
user = r'domain\user'
password = "password"
pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
pm.add_password(None, "http://ftpservice.acesphere.com/", user, password)
auth = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(pm)
opener = urllib2.build_opener(auth)
urllib2.install_opener(opener)
response = urllib2.urlopen(url)
print response.read()
You are getting this exception:
urllib2.HTTPError: HTTP Error 401: Unauthorized
This means that the website is returning an HTTP 401 Unauthorized status code. Either catch the exception or modify your request to not produce this error.
See also: urllib2 documentation