> ## Documentation Index
> Fetch the complete documentation index at: https://docs-dev-docs-event-stream-action-templates.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Applications serveur + API : Implémentation Python pour la tâche Cron

> L’implémentation Python de la tâche Cron du serveur pour le scénario d’architecture Serveur client + API

export const AuthCodeBlock = ({filename, icon, language, highlight, children}) => {
  const [displayText, setDisplayText] = useState(children);
  const [copyText, setCopyText] = useState(children);
  const wrapperRef = React.useRef(null);
  useEffect(() => {
    let unsubscribe = null;
    function init() {
      if (!window.autorun || !window.rootStore) {
        return;
      }
      unsubscribe = window.autorun(() => {
        let processedChildrenForDisplay = children;
        let processedChildrenForCopy = children;
        for (const [key, value] of window.rootStore.variableStore.values.entries()) {
          const escapedKey = key.replaceAll(/[.*+?^${}()|[\]\\]/g, (String.raw)`\$&`);
          let displayValue = value;
          if (key === "{yourClientSecret}" && value !== "{yourClientSecret}") {
            displayValue = value.substring(0, 3) + "*****MASKED*****";
          }
          processedChildrenForDisplay = processedChildrenForDisplay.replaceAll(new RegExp(escapedKey, "g"), displayValue);
          processedChildrenForCopy = processedChildrenForCopy.replaceAll(new RegExp(escapedKey, "g"), value);
        }
        setDisplayText(processedChildrenForDisplay);
        setCopyText(processedChildrenForCopy);
      });
    }
    if (window.rootStore) {
      init();
    } else {
      window.addEventListener("adu:storeReady", init);
    }
    return () => {
      window.removeEventListener("adu:storeReady", init);
      unsubscribe?.();
    };
  }, [children]);
  useEffect(() => {
    if (!wrapperRef.current) return;
    const originalWriteText = navigator.clipboard.writeText.bind(navigator.clipboard);
    let isOverriding = false;
    const handleClick = e => {
      const button = e.target.closest('[data-testid="copy-code-button"]');
      if (!button || !wrapperRef.current.contains(button)) return;
      isOverriding = true;
      navigator.clipboard.writeText = text => {
        if (isOverriding) {
          isOverriding = false;
          navigator.clipboard.writeText = originalWriteText;
          return originalWriteText(copyText);
        }
        return originalWriteText(text);
      };
      setTimeout(() => {
        if (isOverriding) {
          isOverriding = false;
          navigator.clipboard.writeText = originalWriteText;
        }
      }, 100);
    };
    const wrapper = wrapperRef.current;
    wrapper.addEventListener('click', handleClick, true);
    return () => {
      wrapper.removeEventListener('click', handleClick, true);
      if (navigator.clipboard.writeText !== originalWriteText) {
        navigator.clipboard.writeText = originalWriteText;
      }
    };
  }, [copyText]);
  return <div ref={wrapperRef}>
      <CodeBlock filename={filename} icon={icon} language={language} lines highlight={highlight}>
        {displayText}
      </CodeBlock>
    </div>;
};

Dans le cadre du [scénario d’architecture Serveur + API](https://auth0.com/docs/architecture-scenarios/application/server-api), nous expliquerons comment implémenter le processus du serveur dans Python. Veuillez vous référer au document [Scénario d’architecture Serveur + API](https://auth0.com/docs/architecture-scenarios/application/server-api) pour obtenir des informations sur la solution implémentée.

Le code source complet de l’implémentation Python se trouve dans [ce référentiel GitHub](https://github.com/auth0-samples/auth0-pnp-exampleco-timesheets/tree/master/timesheets-cron/python).

## Obtenir un jeton d’accès

Pour effectuer la requête HTTP vers le point de terminaison Auth0 `/oauth/token` de l’API, nous utiliserons les bibliothèques `json`, `urllib` et `urllib2`.

Voici un exemple d’implémentation :

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" />

Pour la tester, modifiez votre code pour imprimer la variable `access_token` et exécutez le processus en utilisant `python cron.py`.

## Invoquer l’API

Les étapes que nous suivons pour l’implémentation sont les suivantes :

* Créer un objet JSON contenant les données de la feuille de temps et affectez-le à une variable `timesheet`.
* Ajoutez l’URL de l’API et le contenu de la variable `timesheet` au corps de la requête en utilisant `urllib2.Request`.
* Ajouter l’en-tête `Authorization` à la requête.
* Définir l’en-tête `Content-Type` sur `application/json`.
* Invoquer l’API en utllisant `urllib2.urlopen` et ajouter des fonctions de gestion des erreurs. Récupérer l’API en utilisant `json.loads`  et imprimez-la dans la console.

Voici un exemple d’implémentation (certains codes sont omis par souci de concision) :

```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
```

Pour la tester, assurez-vous que votre API est en cours d’exécution et lancez le processus en utilisant `python cron.py`.

C’est tout! Vous avez terminé!
