adsabs / adsabs-dev-api

Developer API service description and example client code
162 stars 58 forks source link

curl working but not request #103

Open joelczinn opened 11 months ago

joelczinn commented 11 months ago

Hello,

Thanks so much for this service. I would love to take advantage of it, but I'm running into an authorization issue, it seems.

I would expect the following code to work:

import requests
bibcodes="bibcode\n1907AN....174...59.\n1908PA.....16..445.\n1989LNP...334..242S"
r = requests.post('https://api.adsabs.harvard.edu/v1/search/bigquery',
    params={'q':'*:*', 'wt':'json', 'fq':'{!bitset}', 'fl':'bibcode'},
    headers={'Authorization': 'Bearer:TOKEN'},
    data=bibcodes)
print(r.text)

However, the result is:

'{\n  "error": "Unauthorized"\n}\n'

However, the equivalent request via curl:

curl 'https://api.adsabs.harvard.edu/v1/search/bigquery?q=*:*&fl=bibcode'  -X 'POST' -H 'Authorization: Bearer TOKEN' -H 'Content-Type: big-query/csv' -d $'bibcode\n1907AN....174...59.\n1908PA.....16..445.\n1989LNP...334..242S'

works and returns:

{
  "responseHeader":{
    "status":0,
    "QTime":31,
    "params":{
      "q":"*:*",
      "fl":"bibcode",
      "start":"0",
      "internal_logging_params":"X-Amzn-Trace-Id=Root=1-64fbb58b-52d35276622d99767e24944b",
      "fq":"{!bitset}",
      "rows":"10",
      "wt":"json"}},
  "response":{"numFound":3,"start":0,"docs":[
      {
        "bibcode":"1989LNP...334..242S"},
      {
        "bibcode":"1907AN....174...59."},
      {
        "bibcode":"1908PA.....16..445."}]
  }}

requests is v2.31.0 and Python is v3.8.6.

Thanks in advance for the help!

tjacovich commented 11 months ago

Hello,

Thank you for reaching out to ADS. Looking at the code you provided, I think the issue may be a subtle difference in formatting in this line here:

    headers={'Authorization': 'Bearer:TOKEN'},

The authentication methods are very sensitive to syntax, so this line should really be

    headers={'Authorization': 'Bearer TOKEN'},

like it is in the cURL.

    -H 'Authorization: Bearer TOKEN'

I hope this helps,

-Taylor

joelczinn commented 1 week ago

Hello,

I'm coming back to this to clarify that changing Bearer:TOKEN to Bearer TOKEN did not work... Are there any other ideas of what might be going on?

Thanks again!

tjacovich commented 1 week ago

Hello,

I took the python code you provided and ran it against python-3.8 and requests-v2.31.0 but could not reproduce the issue with my personal token. I modified the code slightly and confirmed this is working.

import requests
TOKEN=REPLACE_TOKEN
bibcodes="bibcode\n1907AN....174...59.\n1908PA.....16..445.\n1989LNP...334..242S"
r = requests.post('https://api.adsabs.harvard.edu/v1/search/bigquery',
    params={'q':'*:*', 'wt':'json', 'fq':'{!bitset}', 'fl':'bibcode'},
    headers={'Authorization': 'Bearer '+str(TOKEN)},
    data=bibcodes)
print(r.text)

For more detailed troubleshooting if this doesn't solve the issue, can you send an email with a detailed description of your current issue, as well as the goal of your queries, to adshelp[at]cfa.harvard.edu?

Thanks,

Taylor