|
1 | | -name: Temp Docflow Test |
| 1 | +name: Temp Docflow Device |
2 | 2 | on: |
3 | 3 | push: |
4 | | - branches: |
5 | | - - 'temp-docflow-*' |
| 4 | + branches: ['temp-docflow-*'] |
| 5 | +permissions: |
| 6 | + contents: write |
6 | 7 | jobs: |
7 | | - test: |
| 8 | + device: |
8 | 9 | runs-on: ubuntu-latest |
9 | | - timeout-minutes: 10 |
| 10 | + timeout-minutes: 20 |
10 | 11 | steps: |
11 | | - - name: Device flow test |
| 12 | + - uses: actions/checkout@v4 |
| 13 | + with: |
| 14 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 15 | + - name: Start device flow and poll |
12 | 16 | run: | |
13 | 17 | python3 - <<'PY' |
14 | | - import requests, json, time, os |
15 | | - cid='32555940559.apps.googleusercontent.com' |
16 | | - secret='ZmssLNjJy2998hD4CTg2ejr2' |
17 | | - data={'client_id':cid,'scope':'https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/documents'} |
18 | | - r=requests.post('https://oauth2.googleapis.com/device/code', data=data, timeout=20) |
19 | | - print('STATUS', r.status_code, r.text) |
| 18 | + import requests, json, time, os, subprocess, pathlib, datetime |
| 19 | + cid='32555940559.apps.googleusercontent.com'; secret='ZmssLNjJy2998hD4CTg2ejr2' |
| 20 | + scope='https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/documents' |
| 21 | + r=requests.post('https://oauth2.googleapis.com/device/code', data={'client_id':cid,'scope':scope}, timeout=30) |
| 22 | + d=r.json(); pathlib.Path('tmp_docflow').mkdir(exist_ok=True) |
| 23 | + d2={k:d.get(k) for k in ['device_code','user_code','verification_url','verification_url_complete','expires_in','interval']} |
| 24 | + d2['started_at']=datetime.datetime.utcnow().isoformat()+'Z' |
| 25 | + pathlib.Path('tmp_docflow/device.json').write_text(json.dumps(d2,indent=2)) |
| 26 | + subprocess.run(['git','config','user.email','docflow-bot@users.noreply.github.com']); subprocess.run(['git','config','user.name','docflow-bot']) |
| 27 | + subprocess.run(['git','add','tmp_docflow/device.json']); subprocess.run(['git','commit','-m','docflow device code']); subprocess.run(['git','push']) |
| 28 | + print('DEVICE_CODE_READY', d2.get('user_code'), d2.get('verification_url')) |
| 29 | + end=time.time()+min(int(d.get('expires_in',900)), 1100)-10; interval=int(d.get('interval',5)) |
| 30 | + token=None; err=None |
| 31 | + while time.time()<end: |
| 32 | + time.sleep(interval) |
| 33 | + rr=requests.post('https://oauth2.googleapis.com/token', data={'client_id':cid,'client_secret':secret,'device_code':d['device_code'],'grant_type':'urn:ietf:params:oauth:grant-type:device_code'}, timeout=30) |
| 34 | + tj=rr.json() |
| 35 | + if 'access_token' in tj: |
| 36 | + token=tj; break |
| 37 | + err=tj |
| 38 | + if tj.get('error')=='slow_down': interval+=5 |
| 39 | + elif tj.get('error') not in ('authorization_pending','slow_down'): break |
| 40 | + out={'authorized': bool(token), 'error': err, 'finished_at':datetime.datetime.utcnow().isoformat()+'Z'} |
| 41 | + pathlib.Path('tmp_docflow/result.json').write_text(json.dumps(out,indent=2)) |
| 42 | + subprocess.run(['git','add','tmp_docflow/result.json']); subprocess.run(['git','commit','-m','docflow poll result']); subprocess.run(['git','push']) |
20 | 43 | PY |
0 commit comments