LoginSignup
1
3

More than 5 years have passed since last update.

GAEでVision APIを呼び出す

Last updated at Posted at 2017-01-22

GAEからVision APIを使ってみました

image_data = user.image.read()
image_content = base64.encodestring(image_data)
headers = {'Content-Type': 'application/json'}
data = {
    "requests": [{
        "image": {
            "content": image_content
        },
        "features": [{
            "type": "SAFE_SEARCH_DETECTION",
            "maxResults": 10
        }]
    }]
}
results = urlfetch.fetch(
    url='https://vision.googleapis.com/v1/images:annotate?key='+GCP_API_KEY,
    headers=headers,
    payload=json.dumps(data),
    method=urlfetch.POST,
    deadline=60,
    validate_certificate=False # SSLのエラーが出るので必要
)
json_results = json.loads(results.content)


(追記)

サービスをリリースするためには、APIキーの制限をしたほうが良いよーとのことなので、設定してみると、なにやらエラーが

Requests from referer <empty> are blocked.

どうも、上記のコードだとRefererが入らないみたいですね

なので、headersRefererを入れてあげちゃうと良さげ

headers = {
    'Content-Type': 'application/json',
    'Referer': 'your domain here'
}
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3