Open4
google auth & spreadsheets download as csv with postman
- create project in gcp
- enable google drive api link
- create OAuth consent window. [link] (https://console.cloud.google.com/apis/credentials/consent)
- external user
- Google Drive API (auth/drive.readonly)
- create OAuth 2.0 client id. link
- callback url: https://oauth.pstmn.io/v1/callback
- memo client id and client secret.
- install Postman
- Postman setting OAuth2
- Authorization > Type OAuth 2.0
- configure new token
- Auth URL: https://accounts.google.com/o/oauth2/v2/auth?access_type=offline
- Auth Token URL: https://oauth2.googleapis.com/token
- Client ID:
- Client Secret:
- add scope https://www.googleapis.com/auth/drive.readonly
- memo access token
- request spread sheet
- https://drive.google.com/spreadsheets/d/{id}/export?format=csv&gid={seetid}
- add Authorization header with: "Bearer {AccessToken}"
refresh token
curl -X POST -d "grant_type=refresh_token" \
-d "refresh_token={your_refresh_token}" \
-d "client_id={your_client_id}" \
-d "client_secret={your_client_secret"} \
-H "Content-Type: application/x-www-form-urlencoded" \
https://oauth2.googleapis.com/token
in scrapy
crawl 時に settingsを渡すとよいかも・・・
ScrapingHub利用時は、クレデンシャル類は、DBなどで管理し、ScrpingHubのAPI に渡すのが楽かも
# in spider class attributes
custom_settings = {
"ROBOTS_OBEY": False,
}
# in start_requests
yield scrapy.Request(
url="https://oauth2.googleapis.com/token",
body="grant_type=refresh_token&refresh_token=...&client_id=...&client_secret=...",
headers={"Content-Type": "application/x-www-form-urlencoded"},
method="POST",
callback=self.parse
)
# in parse method
response = json.loads(response.text)
response["access_token"]
いつも、このドキュメントにたどり着けないので
python で SDK使うならこれ
今回は scrapy 中で使いたかったのでREST API利用