> ## Documentation Index
> Fetch the complete documentation index at: https://auth0.generaltranslation.app/llms.txt
> Use this file to discover all available pages before exploring further.

# サーバーアプリ＋API：Pythonによるcronジョブの実装

> サーバークライアント＋APIアーキテクチャシナリオ向けのサーバーcronジョブのPython実装

export const AuthCodeBlock = ({filename, icon, language, highlight, children}) => {
  const [processedChildren, setProcessedChildren] = useState(children);
  useEffect(() => {
    let unsubscribe = null;
    function init() {
      unsubscribe = window.autorun(() => {
        let processedChildren = children;
        for (const [key, value] of window.rootStore.variableStore.values.entries()) {
          processedChildren = processedChildren.replace(new RegExp(key, "g"), value);
        }
        setProcessedChildren(processedChildren);
      });
    }
    if (window.rootStore) {
      init();
    } else {
      window.addEventListener("adu:storeReady", init);
    }
    return () => {
      window.removeEventListener("adu:storeReady", init);
      unsubscribe?.();
    };
  }, [children]);
  return <CodeBlock filename={filename} icon={icon} language={language} lines highlight={highlight}>
      {processedChildren}
    </CodeBlock>;
};

[サーバー＋APIアーキテクチャシナリオ](https://auth0.com/docs/architecture-scenarios/application/server-api)」のドキュメントを参照してください。

Pythonでのサーバープロセス実装で使用する全ソースコードは、[こちらのGitHubリポジトリ](https://github.com/auth0-samples/auth0-pnp-exampleco-timesheets/tree/master/timesheets-cron/python)でご覧いただけます。

<div id="get-an-access-token">
  ## アクセストークン取得する
</div>

HTTP要求をAuth0の`/oauth/token` APIエンドポイントに行うため、`json`、`urllib`、および`urllib2`のライブラリーを使用します。

以下が実装例です。

export const codeExample = `def main():
  import json, urllib, urllib2, httplib

  # Configuration Values
  domain = "{yourDomain}" # Your Auth0 Domain
  api_identifier = "API_IDENTIFIER" # API Identifier of your API
  client_id = "{yourClientId}" # Client ID of your Machine-to-Machine Application
  client_secret = "{yourClientSecret}" # Client Secret of your Machine to Machine Application
  api_url = "http://localhost:8080/timesheets/upload"
  grant_type = "client_credentials" # OAuth 2.0 flow to use

  # Get an access token from Auth0
  base_url = "https://{domain}".format(domain=domain)
  data = urllib.urlencode({'client_id': client_id,
                            'client_secret': client_secret,
                            'audience': api_identifier,
                            'grant_type': grant_type})
  req = urllib2.Request(base_url + "/oauth/token", data, headers={"Accept": "application/x-www-form-urlencoded"})
  response = urllib2.urlopen(req)
  resp_body = response.read()
  oauth = json.loads(resp_body)
  access_token = oauth['access_token']

# Standard boilerplate to call the main() function.
if __name__ == '__main__':
  main()`;

<AuthCodeBlock children={codeExample} language="python" />

テストするには、コードを変更して`access_token`変数を表示し、`python cron.py`を使用してプロセスを実行します。

<div id="invoke-the-api">
  ## APIを呼び出す
</div>

以下の手順で実装を行います。

* タイムシートデータを含むJSONオブジェクトを構築し、`timesheet`変数に割り当てます。
* `urllib2.Request`を使用して、API URLと`timesheet`変数コンテンツを要求本文に追加します。
* `Authorization`ヘッダーを要求に追加します。
* `Content-Type`ヘッダーを`application/json`に設定します。
* `urllib2.urlopen`を使用してAPIを呼び出し、いくつかのエラー処理を追加します。`json.loads`を使用して応答を取得し、コンソールに表示します。

以下が実装例です（簡潔にするため一部のコードを省略しています）。

```python lines theme={null}
def main():
  # import libraries - code omitted

  # Configuration Values - code omitted

  # Get an Access Token from Auth0 - code omitted

  #Post new timesheet to API
  timesheet = {'user_id': '007',
                          'date': '2017-05-10T17:40:20.095Z',
                          'project': 'StoreZero',
                          'hours': 5}
  req = urllib2.Request(api_url, data = json.dumps(timesheet))
  req.add_header('Authorization', 'Bearer ' + access_token)
  req.add_header('Content-Type', 'application/json')

  try:
    response = urllib2.urlopen(req)
    res = json.loads(response.read())
    print 'Created timesheet ' + str(res['id']) + ' for employee ' + str(res['user_id'])
  except urllib2.HTTPError, e:
    print 'HTTPError = ' + str(e.code) + ' ' + str(e.reason)
  except urllib2.URLError, e:
    print 'URLError = ' + str(e.reason)
  except httplib.HTTPException, e:
    print 'HTTPException'
  except Exception, e:
    print 'Generic Exception' + str(e)

# Standard boilerplate to call the main() function - code omitted
```

これをテストするには、APIが稼働しており、`python cron.py`を使用してプロセスを実行していることを確認してください。

以上で完了です。
