Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

curl working but not request #103

Open
joelczinn opened this issue Sep 9, 2023 · 3 comments
Open

curl working but not request #103

joelczinn opened this issue Sep 9, 2023 · 3 comments

Comments

@joelczinn
Copy link

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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

@tjacovich tjacovich reopened this Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants