You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
##### Authorizing with existing OAuth Token (advanced)
84
-
85
-
An existing token can be provided when creating the farmOS client. This is
86
-
useful for advanced use cases where an OAuth token may be persisted.
87
-
88
-
```python
89
-
from farmOS import farmOS
90
-
91
-
hostname ="myfarm.farmos.net"
92
-
token = {
93
-
"access_token": "abcd",
94
-
"refresh_token": "abcd",
95
-
"expires_at": "timestamp",
96
-
}
97
-
98
-
# Create the client with existing token.
99
-
farm_client = farmOS(
100
-
hostname=hostname,
101
-
token=token,
102
-
)
103
-
```
104
-
105
-
##### Saving OAuth Tokens
106
-
107
-
By default, access tokens expire in 1 hour. This means that requests sent 1
108
-
hour after authorization will trigger a `refresh` flow, providing the client
109
-
with a new `access_token` to use. A `token_updater` can be provided to save
110
-
tokens external of the session when automatic refreshing occurs.
111
-
112
-
The `token_updater` defaults to an empty lambda function: `lambda new_token: None`.
113
-
Alternatively, set `token_updater = None` to allow the [`requests_oauthlib.TokenUpdated`](https://requests-oauthlib.readthedocs.io/en/latest/api.html#requests_oauthlib.TokenUpdated)
114
-
exception to be raised and caught by code executing requests from farmOS.py.
115
-
116
-
```python
117
-
from farmOS import farmOS
118
-
119
-
hostname ="myfarm.farmos.net"
120
-
username ="username"
121
-
password ="password"
122
-
123
-
# Maintain an external state of the token.
124
-
current_token =None
125
-
126
-
# Callback function to save new tokens.
127
-
deftoken_updater(new_token):
128
-
print(f"Got a new token! {new_token}")
129
-
# Update state.
130
-
current_token = new_token
131
-
132
-
# Create the client.
133
-
farm_client = farmOS(
134
-
hostname=hostname,
135
-
token_updater=token_updater, # Provide the token updater callback.
## Authorizing with existing OAuth Token (advanced)
46
+
47
+
An existing token can be provided when creating the farmOS client. This is
48
+
useful for advanced use cases where an OAuth token may be persisted.
49
+
50
+
```python
51
+
from farmOS import farmOS
52
+
53
+
hostname ="myfarm.farmos.net"
54
+
token = {
55
+
"access_token": "abcd",
56
+
"refresh_token": "abcd",
57
+
"expires_at": "timestamp",
58
+
}
59
+
60
+
# Create the client with existing token.
61
+
farm_client = farmOS(
62
+
hostname=hostname,
63
+
token=token,
64
+
)
65
+
```
66
+
67
+
## Saving OAuth Tokens
68
+
69
+
By default, access tokens expire in 1 hour. This means that requests sent 1
70
+
hour after authorization will trigger a `refresh` flow, providing the client
71
+
with a new `access_token` to use. A `token_updater` can be provided to save
72
+
tokens external of the session when automatic refreshing occurs.
73
+
74
+
The `token_updater` defaults to an empty lambda function: `lambda new_token: None`.
75
+
Alternatively, set `token_updater = None` to allow the [`requests_oauthlib.TokenUpdated`](https://requests-oauthlib.readthedocs.io/en/latest/api.html#requests_oauthlib.TokenUpdated)
76
+
exception to be raised and caught by code executing requests from farmOS.py.
77
+
78
+
```python
79
+
from farmOS import farmOS
80
+
81
+
hostname ="myfarm.farmos.net"
82
+
username ="username"
83
+
password ="password"
84
+
85
+
# Maintain an external state of the token.
86
+
current_token =None
87
+
88
+
# Callback function to save new tokens.
89
+
deftoken_updater(new_token):
90
+
print(f"Got a new token! {new_token}")
91
+
# Update state.
92
+
current_token = new_token
93
+
94
+
# Create the client.
95
+
farm_client = farmOS(
96
+
hostname=hostname,
97
+
token_updater=token_updater, # Provide the token updater callback.
0 commit comments