updating user-fields through Directory API (admin sdk) - python

I'm working with Google Directory API (admin sdk) through python and some user updates seem to be doing less than expected. I want to update addresses/emails/...-fields, overwriting the list with an empty one.
I tested in Try it! and even that doesn't work:
The result is 200 OK and everything seem to work until a get-request reviles that nothing has happened.

I just tried to reproduce this and i was able to remove addresses after a few failed attempts.
This is what had to be done in "Try it!"
Switch to the "Freeform Editor". There's a dropdown in the box for the request body.
As request body send
{
"addresses": [{}]
}
Why doesn't it work for you? Since you use the structured editor your request is enclose in double quotes ("") thus the adress is a big mess and nothing happens except you get a really messed up response from the update endpoint. Why in the world google would return a status code 200 (OK) for that evades me.
If you transfer this learning to your code it should work. Sorry i don't grok python so i can't help with the python code.

Related

Getting very weird 400 errors (Imgur API)

I know how this may read to you but I am seriously out of ideas on this one.
I've written something in Python that can download stuff from Imgur using their API. I have an Authorization clientID and everything, and this thing works.
But sometimes I am getting a 400 HTML response status code with empty text when requesting a direct link so I can save the file. According to the doc this error will come back at you when you're missing a required parameter or if a part of your request is incorrect; this feels like a weird error to come back at me, when other requests get through just fine. The weirder part is that I can send 100 requests in 100 seconds, all will come back with 400 None. But once I open the image in my browser and view it, the request suddently changes to 200, everything works and this link will never throw a 400 ever again.
My friend suggestet that maybe it had something to do with my IP adress (getting flagged as a spam bot or whatever), so I opened the URLs via my phone on cellular, but as soon as the image loaded on my phone, the requests were successful. Also, when I tried finding more links with this strange behaviour, I tested a bunch of images from the imgur front page, and all worked fine. Combining this with the fact that I got the problematic links from very old reddit threads leaves me with the only idea that it has something to do with the age of the files or rather their last view date.
The requests happened via a code similar to:
headers = {'Authorization': f"Client-ID <id>"}
url = f"https://api.imgur.com/3/image/{fileID}"
requests.get(url, headers=headers)
I'm asking this here mainly because the Imgur API page says the best way to get help about the API is to post the problem here on stackoverflow. Maybe one of their engineers sees this and can answer my question or maybe someone else has an idea what may be going on here. In any case, I would be grateful for any useful input ^^

how to download a video from a twitch clip in python [duplicate]

I have been trying to download past broadcasts for a streamer on twitch using python. I found this python code online:
https://gist.github.com/baderj/8340312
However, when I try to call the functions I am getting errors giving me a status 400 message.
Unsure if this is the code I want to download the video (as an mp4) or how to use it properly.
And by video I mean something like this as an example: www(dot)twitch.tv/imaqtpie/v/108909385 //note cant put more than 3 links since I don't have 10 reputation
Any tips on how i should go about doing this?
Here's an example of running it in cmd:
python twitch_past_broadcast_downloader.py 108909385
After running it, it gave me this:
Exception API returned 400
This is where i got the information on running it:
https://www.johannesbader.ch/2014/01/find-video-url-of-twitch-tv-live-streams-or-past-broadcasts/
Huh it's not as easy at it seems ... The code you found on this gist is quite old and Twitch has completely changed its API. Now you will need a Client ID to download videos, in order to limit the amount of video you're downloading.
If you want to correct this gist, here are simple steps you can do :
Register an application : Everything is explained here ! Register you app and keep closely your client id.
Change API route : It is no longer '{base}/api/videos/a{id_}' but {base}/kraken/videos/{id_} (not sure about the last one). You will need to change it inside the python code. The doc is here.
Add the client id to the url : As said in the doc, you need to give a header to the request you make, so add a Client-ID: <client_id> header in the request.
And now I think you will need to start debugging a bit, because it is old code :/
I will try myself to do it and I will edit this answer when I'm finished, but try yourself :)
See ya !
EDIT : Mhhh ... It doesn't seem to be possible anyway to download a video with the API :/ I was thinking only links to API changed, but the chunks section of the response from the video url disappeared and Twitch is no longer giving access to raw videos :/
Really sorry I told you to do that, even with the API I think is no longer possible :/
You can download past broadcasts of Twitch videos with Python library streamlink.
You will need OAuth token which you can generate with the command
streamlink --twitch-oauth-authenticate
Download VODs with:
streamlink --twitch-oauth-token <your-oauth-token> https://www.twitch.tv/videos/<VideoID> best -o <your-output-folder>

Why don't I get a response from my request?

I'm trying to make one simple request:
ua=UserAgent()
req = requests.get('https://www.casasbahia.com.br/' , headers={'User-Agent':ua.random})
I would understand if I received <Response [403] or something like that, but instead, a recive nothing, the code keep runing with no response.
using logging I see:
I know I could use a timeout to avoid keeping the code running, but I just want to understand why I don't get an response
thanks in advance
I never used this API before, but from what I researched on here just now, there are sites that can block requests from fake users.
So, for reproducing this example on my PC, I installed fake_useragent and requests modules on my Python 3.10, and tried to execute your script. It turns out that with my Authentic UserAgent string, the request can be done. When printed on the console, req.text shows the entire HTML file received from the request.
But if I try again with a fake user agent, using ua.random, it fails. The site was probably developed to detect and reject requests from fake agents (or bots).
Though again, this is just theory. I have no ways to access this site's server files to comprove it.

Does a Python get request to an asp page impact on the response?

I'm trying to download a bundle of reports (PDF and XLSX) from a website.
I had a working solution that is now failing as the important GET request is now responding with
{'result': 'redirect', 'url': '../warning.asp?header=Report has expired&body=Your report has already expired. Please select a report from the menu.&footer='}
Executing the necessary login, report selection and output steps in postman works perfectly. The same under python is giving me the above problem.
I can login into the actual website and trace the various steps. I note that one step opens a new browser window and then automatically starts to download the report.
The code is pretty basic
rw = session.get('https://website.com.au/Reports/ReportWindow.asp?' + report_urlencoded)
if rw.status_code == 200:
print('https://website.com.au/reports/generatereport.asp?_=' + str(current_milli_time()))
gr = session.get('https://website.com.au/reports/generatereport.asp?_=' + str(current_milli_time()))
The line
rw = session.get('https://website.com.au/Reports/ReportWindow.asp?' + report_urlencoded)
opens a new window and the automatically proceeds to download the requested report (if I use an actual browswer)
The above run in postman does exactly what I want it do.
What I am missing here?
Thanks
I can't quite work out what I did to fix it, but I suspect that sending some fairly standard headers as part of the request has probably fixed it.
It may also been that I was not properly logged in despite getting back 200 response (which I had assumed was logged in but maybe not)

Malformed token - facebook API using Python. Encoding issue?

I've playing around in Python and the Facebook API - I'm fairly new to it! I'm copying and pasting an access token into my Python app from the Graph API Explorer as oauth_access_token manually just before execution. The simple app is a loop which performs some actions based on the status of some inputs. The loop also refreshes the token so that once the app is started with a new token, it should keep going for a longer amount of time. Snippet posted below, with IDs anonymized:
oauth_access_token="ABCDEFGHIJL"
auth_str="/oauth/access_token?grant_type=fb_exchange_token&client_id=XYZ1ABC&client_secret=[ABC2XYZ&fb_exchange_token="+oauth_access_token
graph = facebook.GraphAPI(oauth_access_token)
oauth_access_token = str(graph.request(auth_str)['access_token'])
print oauth_access_token
profile = graph.request("/me/notifications")
profile2 = graph.request("/me/inbox?fields=unseen")
I realize this isn't particularly elegant way of doing it. Bizarrely, when I do this on Windows, it works fine and I have access to the my notifcations and messages. When I do this on my Mac or Ubunutu it doesn't work and I get this error...I think it has something to do with Encoding, but I'm not sure what nor how to fix it?
Malformed access token ABCDEFGHIJK?access_token=ABCDEFGHIJK

Categories

Resources