I wrote a bit about it in this Programming Historian lesson:
https://programminghistorian.org/en/lessons/fetch-and-parse-data-with-openrefine#example-3-advanced-apis
(sorry, I need to update this lesson sometime!)
Basically, it depends on how the service wants the API key passed,
sometimes as headers or sometimes as a data value. Refine's Jython is 2.7,
and there is a good "how to" doc with examples of both cases available
here:
https://docs.python.org/2/howto/urllib2.html
If the api key is one of the data values (which seems to be in your curl
example), you can pass an array of key/value pairs to urllib request
object, something like (from the howto above):
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
data = urllib.urlencode(values)req = urllib2.Request(url, data)
If you need to pass the api key as a header, you add another value to the
request object, something like:
headers = {'User-Agent': user_agent}req = urllib2.Request(url, data, headers)
On Wed, Jan 1, 2020 at 7:59 PM CODE4LIB automatic digest system <
[log in to unmask]> wrote:
> There is 1 message totaling 35 lines in this issue.
>
> Topics of the day:
>
> 1. API key handling in python/jython
>
> ----------------------------------------------------------------------
>
> Date: Wed, 1 Jan 2020 15:12:30 +0530
> From: Parthasarathi Mukhopadhyay <[log in to unmask]>
> Subject: API key handling in python/jython
>
> Dear All
>
> I am trying to pass python/jython expression in Openrefine for sentiment
> analysis for a textual data column. The service supports non-GET call like:
>
> curl -X POST -F 'text=["Come on,lets play together",
> "Team performed well overall."]' -F
> 'api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxx'
> https://apis.paralleldots.com/v4/sentiment_batch
>
> How this can be reformatted inside python/jython expression in Openrefine?
>
> I am able to convert such expression where there is no API key
> authentication
> required (like NLTK - https://text-processing.com/) but don't have any
> clue how to handle API key in
> post=urllib2.urlopen().
>
> Help is solicited...
>
> Regards
>
> -----------------------------------------------------------------------
> Dr. Parthasarathi Mukhopadhyay
> Professor, Department of Library and Information Science,
> University of Kalyani, Kalyani - 741 235 (WB), India
> -----------------------------------------------------------------------
>
> ------------------------------
>
> End of CODE4LIB Digest - 30 Dec 2019 to 1 Jan 2020 (#2020-1)
> ************************************************************
>
|