Using API authentication with shared python code - python

I am working on a python script that uses github's api. I intend to share the code with someone to show them how the script works.
Because I cross github's query limit as an anonymous user, I need to have an authentication. What form of authentication should I use in my script since I am sharing it?

Like #Hang stated, I used github's personal access token. This is what I did.
token='my_token'
url='https://api.github.com/search/repositories?q=python'
rslt=requests.get(url,headers={'Authorization':auth})

Related

python - how to access an API from azurewebsites

I am trying to access an API which is at azurewebsites.net. I am new to Azure platform and I don't know if for accessing this API through my code, would I need any additional configurations in the Azure platform? I tried the normal request method to get the API data in python but it throws an authentication error. Do I need to register my app in the Azure?
Here's what I am trying:
r = requests.get("url",
headers={
"Accept": "application/json"},
cookies={},
auth=('email', 'pass'),
)
Could someone please guide me through this? Thankyou.
Because I don't know what settings your azure website has done, so I can only give you a general solution.
Steps to try:
First of all, make sure that you can access your api site normally. You can test other interfaces to ensure that the api site is running normally.
When the website is running normally, check whether the aad setting is made on the portal.
If not, please use postman to test the interface to ensure that it can be accessed normally in postman, and then use the code to test by checking the url, header and other parameters.
😋If yes, please obtain Beartoken and add it when accessing the interface. I see that you are using email and pass, then you can use ropc flow get Beartoken. Then you can bring this verification when you visit each interface.
Related post:
Is there a way to improve the performance of MSAL-browser js login?

Make the Google python client library for accessing Google cloud storage hit a stubbed API

I am writing an application that uses Google's python client for GCS.
https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-python
I've had no issues using this, until I needed to write my functional tests.
The way our organization tests integrations like this is to write a simple stub of the API endpoints I hit, and point the Google client library (in this case) to my stub, instead of needing to hit Google's live endpoints.
I'm using a service account for authentication and am able to point the client at my stub when fetching a token because it gets that value from the service account's json key that you get when you create the service account.
What I don't seem able to do is point the client library at my stubbed API instead of making calls directly to Google.
Some work arounds that I've though of, that I don't like are:
- Allow the tests to hit the live endpoints.
- Put in some configuration that toggles using the real Google client library, or a mocked version of the library. I'd rather mock the API versus having mock code deployed to production.
Any help with this is greatly appreciated.
I’ve made some research and it seems like there’s nothing supported specifically for Cloud Storage using python. I found this GitHub issue entry with a related discussion, but for go.
I think you can open a public issue tracker asking for this functionality. I’m afraid by now it’s easier to keep using your second workaround.

How to query a private table in google-bigquery from a python script WITHOUT authorization token?

I'm trying to submit a query into Google's BigQuery and retrieve results - all from a python script. While there's straightforward documentation on doing so, the only option that I've found for querying from private tables/collections is to use an authorization code. However, this python script is utilized via a webpage used by users who know nothing about code - therefore there is no room to get/submit authorization codes, as the user simply uses the webpage and python script by clicking a few buttons. Is there any way to get the authorization code and submit it behind the scenes, or to query a private table without an authorization code altogether (best option)? Thanks so much!
You can use a service account:
Client libraries can use Application Default Credentials to easily
authenticate with Google APIs and send requests to those APIs. With
Application Default Credentials, you can test your application locally
and deploy it without changing the underlying code
https://cloud.google.com/bigquery/authentication#bigquery-authentication-python

Do I need OAuth process if my GAE app should use Analytics API?

I would like to display on my GAE website a number of daily users of my android application (tracked with Google Analytics), both are under the same account. I wanted to use Analytics API for this. I am reading Hello Analytics API tutorial and looks like I need OAuth to authorize my app to get the data from Analytics. It looks quite complex, is there any easy way (like getting some token and using it)?
Looks like I need service account (for server to server applications), but I don't understand how to modify starter application (taken there) to use it.
Since you only want to access data that you own, using a service account is a good idea.
How ever the starter application you linked is for working with a normal OAuth2 and its not the same as the code for a service account.
I haven't done this in python myself before, but i found an example that should get you started. You will need to change it slightly because the scopes are for Google+ not Google analytics. create the service in python on GAE
If you have any problems changing it to work with Google Analtyics let me know i should be able to help with that. Even if i cant test the python code myself.
There is much simpler way to pass authorization - see App Identity doc. See also Java example how to access Analytics data from the GAE.
Another useful link is here.

Testing Django Facebook App

I'm making a Django app with Fandjango and I'm trying to unit test it with Django's test framework. The only thing is, in order to test effectively I need a "signed_request" parameter that Facebook sends with every request. Right now I'm logging the requests my server gets from Facebook and copying + pasting the signed_request token I get, but that only works for a few hours at a time.
Is there a simple way to handle this without doing a mock of the whole Facebook API?
Thanks!
You can use Test Users:
http://developers.facebook.com/docs/test_users/
I think the access token never expires, or at less until you delete the Test User.
Well, I understand it's also possible to authenticate fully server side, using just OAuth without Javascript SDK. In that case you should be able to aquire a valid token yourself. There are, I think some libraries that can be used for that like:
http://pypi.python.org/pypi/django-social-auth/
However please note, I've never done this myself so it's more of a suggestion, than a definite answer.
EDIT
It seems like social-auth has some testing functionality that is capable of automatically signing in to a facebook account. You could probably copy the code from there.

Categories