How do I receive Oanda stream using oanda API? - stream

I'd like to receive trading information using Oanda-v20-API. On the site it says that I can use rest-api to get a stream, but I can't find out how to do it using their liibrary?
Or using jax-rs?
Thanks.

try this in python 3 or put it into a jupyter notebook
import requests
import json
headers = {'Content-Type': 'application/json',
"Authorization": "Bearer <<YOUR ACCESS CODE HERE>>"}
# Streaming prices
baseurl = 'https://stream-fxpractice.oanda.com/v3/accounts/<<your account id here>>/pricing/stream'
payload = { 'instruments' : 'EUR_USD'}
r = requests.get(baseurl, params=payload, headers=headers, stream=True)
print(r.headers)
print('\n')
for line in r.iter_lines():
if line:
print(json.loads(line.decode("utf-8")))

Related

How to authenticate a call to a private google cloud function from a container on a VM on the vpc?

In GCP, I have a Compute Engine VM on the default VPC running docker. I have a container app (python fastAPI web app) that needs to call a private cloud function to post some data using requests.
head = {
'Content-Type': 'application/json;charset=UTF-8',
'Accept': 'application/json',
'Authorization': 'Bearer ' + token
}
resp = requests.post(url,
data = msgObj,
headers = head,
timeout=1.50
)
This is all working fine, but the call to the private cloud function needs Bearer Authorization. I can't seem to find a way to programmatically get this token. I can get a token for testing by using
gcloud auth print-identity-token > token.txt on the host VM. This works, but is not acceptable for production use.
Any thoughts?
I have tried to use a token generated by another private cloud function that posts data to the container app.
const auth = new GoogleAuth();
const token = await auth.getAccessToken();
But this token didn't work. (401 Unauthorized)
Getting the token via gcloud (see above) from the VM host works, but is obviously hardcoded and will not work for long - only testing.
This use case (calling a private cloud function from a container) doesn't seem to be covered by anything I can find. There is a video on how to do this from google but was useless - basically saying to use the gcloud approach above.
The answer was actually straightforward once I found it from several clues.
add google-auth=2.6.2 to the requirements doc for the container
add the following to your container code
import google.auth.transport.requests
import google.oauth2.id_token
audience = function_url
auth_req = google.auth.transport.requests.Request()
id_token = google.oauth2.id_token.fetch_id_token(auth_req, audience)
Then use the id_token in the header for the function call
head = {
'Content-Type': 'application/json;charset=UTF-8',
'Accept': 'application/json',
'Authorization': 'Bearer ' + id_token
}
resp = requests.post(audience,
data = msgObj,
headers = head
)

A Python code used to get the parent message of a slack thread not working

I wanted to make an API call to get the parent message of a thread using the thread ID on zapier
This is the code I got by chatgpt to do that, I am getting a no output error and I am not sure why it happens
import requests
def main(input_data):
# Set the API endpoint, headers, and Slack API token for the request
api_endpoint = "https://slack.com/api/conversations.replies"
headers = {
"Authorization": "Bearer xoxb-3482548707638-4570295651095-8J4mDrROrcYV0U3L5FZhJtTf",
"Content-Type": "application/json; charset=utf-8"
}
slack_api_token = "xoxb-3482548707638-4570295651095-8J4mDrROrcYV0U3L5FZhJtTf"
# Set the request parameters
params = {
"channel": input_data["thread_id"],
"ts": input_data["thread_id"],
"inclusive": "true",
"limit": 1
}
# Make a GET request to the API endpoint
response = requests.get(api_endpoint, headers=headers, params=params)
# Get the first message in the result
message = response.json()["messages"][0]
# Print the message text to the console
print(message["text"])
This my first time working with API's or code, so I am pretty sure I mus have done something stupid. Could anyone point out what I did wrong
PS: After this, what should I learn to operate such codes, I find this interesting

What are the ways to provide authorization(Oauth 2.0) for the user in JIRA SOFTWARE TOOL?

Following are the steps I had followed:
1. Make project through Jira.
2. Followed all the steps and got autharization url.
3. now able to access access_token and refresh_token.
Problem:
In oauth 2.0 from one admin client id and client secret we can authorize multiple accounts registered with JIRA Software.
But I am not able to do this. I am not able to authorize the the users as what I did while integrating my project with google services like task, sheet, contacts etc.
I will be providing redirect code:
(I made a redirect api and registered its url in the setting tab)
class redirect(APIView):
def get(self, request):
code = request.GET['code']
headers = {
'Content-Type': 'application/json',
}
data = {"grant_type": "authorization_code", "client_id": "gJZQ66IW6h7ubMTnFuJmR5EnWgYEyadK",
"client_secret": "KA2gCS80bjaNp-plqZ9twafqhyiuDeTYWGx6WByjNHsnGEHVPC5JFByC4ubGpVfU", "code": code,
"redirect_uri": "http://localhost:8000/bug/redirect"}
response = requests.post('https://auth.atlassian.com/oauth/token', headers=headers, data=json.dumps(data))
# access_token = response.json()['access_token']
refresh_token = response.json()['refresh_token']
header = {
'Content-Type': 'application/json',
}
access_token = response.json()['access_token']
import pdb
pdb.set_trace()
headers = {
'Authorization': access_token,
'Accept': 'application/json',
}
response = requests.get('https://api.atlassian.com/oauth/token/accessible-resources', headers=headers)
return JsonResponse({'response': response.json()})

Request to Machine Learning service on Bluemix using Python

I'm struggling in find a way to send data and have back the prediction of a SPSS model deployed on Bluemix Machine Learning service.
I make a lot of test using request library in Python or with curl command but I did not succeed.
I'm too new to Bluemix to understand the service documentation.
Any help,
Thanks
I managed to pass data in and receive the prediction with the code as follows:
import requests, urllib3, json
access_key= "INSERT_ACCESS_KEY_HERE"
username = "INSERT_USERNAME_HERE"
password = "INSERT_PASSWORD_HERE"
headers = urllib3.util.make_headers(basic_auth='{}:{}'.format(username, password))
payload_online= { "tablename": "INSERT_TABLENAME_HERE", "header": [INSERT_TABLE_HEADERS_HERE],"data": [[INSERT_DATA_TO_USE_FOR_THE_PREDICTION_HERE]]}
url= 'https://ibm-watson-ml.mybluemix.net/pm/v1/score/INSERT_CONTEXTID_HERE?accesskey=INSERT_THE_ACCESS_KEY'
header = {'Content-Type': 'application/json', 'Authorization': "INSERT_TOKEN_HERE"}
response_online = requests.post(url, json=payload_online, headers=header)
print(response_online.text)

hipchat oauth token internal server error

I follow hipchat site and try to get an add-on token
https://developer.atlassian.com/hipchat/guide/hipchat-rest-api/api-access-tokens?_ga=2.94621780.975509804.1497491262-871504840.1479901346#APIaccesstokens-Add-ongeneratedtokens
I have the following code:
import requests
secret = 'hipchat_secret'
headers = {'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic {}'.format(secret)}
url = 'https://api.hipchat.com/v2/oauth/token?grant_type=client_credentials&scope=send_notification+admin_room'
res = requests.post(url, headers=headers)
But it gives me a 500 Internal Server Error. What is wrong with my code?
The mistake was I used v1 token.
It works perfect when I switch to v2

Resources