Skip to content

Commit

Permalink
fix needing to copy token.json to working dir
Browse files Browse the repository at this point in the history
  • Loading branch information
utkuufuk committed Nov 7, 2018
1 parent 2a2423c commit 02affb7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ From project directory:
```

## Usage
You can execute the **`budget`** command from anywhere **except directories that you don't have write permission.** *(Hopefully this will be fixed soon, see [issue](https://github.com/utkuufuk/google-budget/issues/2))*
You can execute the **`budget`** command globally for spreadsheet selection and transaction entry.

### Spreadsheet Selection
``` sh
Expand Down
12 changes: 7 additions & 5 deletions budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import os
import shutil

GLOBAL_TOKEN_PATH = str(Path.home()) + '/.local/share/google-budget/token.json'
SPREADSHEET_ID_PATH = str(Path.home()) + '/.local/share/google-budget/spreadsheet.id'
APP_DIR = str(Path.home()) + '/.local/share/google-budget/'
GLOBAL_TOKEN_PATH = APP_DIR + 'token.json'
SPREADSHEET_ID_PATH = APP_DIR + 'spreadsheet.id'

if __name__ == '__main__':
# validate command
Expand Down Expand Up @@ -43,12 +44,14 @@
print("budget sheet <spreadsheet_id>", file=sys.stderr)
sys.exit(1)

# temporarily copy the auth token into current dir and authorize
shutil.copyfile(GLOBAL_TOKEN_PATH, 'token.json')
# temporarily change working directory to read token.json and authorize
initialDir = os.getcwd()
os.chdir(APP_DIR)
store = file.Storage('token.json')
creds = store.get()
service = build('sheets', 'v4', http=creds.authorize(Http()))
print("Authorization successful.")
os.chdir(initialDir)

# fetch existing data in order to find the index of the last transaction
try:
Expand All @@ -71,4 +74,3 @@
result = service.spreadsheets().values().update(spreadsheetId=ssheetId, range=rangeName,
valueInputOption="USER_ENTERED", body=body).execute()
print('{0} cells updated.'.format(result.get('updatedCells')))
os.remove('token.json')

0 comments on commit 02affb7

Please sign in to comment.