Upgrade to Pro — share decks privately, control downloads, hide ads and more …

go-gmail-drafts

 go-gmail-drafts

94500f2d1984cd5da6e445d89819052b?s=128

Yoshiki Nakagawa

January 22, 2016
Tweet

Transcript

  1. ΦʔϓϯιʔεϥΠϒϥϦݚڀձ #3 Jan / 22 / 2016 Yoshiki Nakagawa eureka,

    Inc.
  2. AboutMe @yyoshiki41 Yoshiki Nakagawa Infrastructure Engineer, pairs Division Tech •

    Golang • Middleware, RDBMS • AWS
  3. Google APIs Client Library for Go GitHub : https://github.com/google/google-api-go-client/ Enable

    to access google APIs - Gmail API - BigQuery API - Calendar API etc...
  4. yyoshiki41/go-gmail-drafts GitHub : https://github.com/yyoshiki41/go-gmail-drafts Create a new draft using the

    Gmail API.
  5. Why not Google Apps Script ? Apps Script is not

    enough... Can't create drafts. (only get drafts) So use Google APIs Client Library ! Other languages libraries : https://developers.google.com/discovery/libraries All Libraries are open-sourced !
  6. Preparation ̍. Google Developer Console ͔Βɺ৽نϓϩδΣΫτΛ࡞੒ ͢Δɻ ̎. ϓϩδΣΫτͷ Gmail

    API Λ༗ޮʹ͢Δɻ
  7. ̏. ೝূ৘ใΛઃఆ͢Δɻ ̐. ඞཁͳೝূ৘ใΛΫϦοΫɻ

  8. ̑. OAuth ΫϥΠΞϯτͷઃఆɻ OAuthೝূΛ׬ྃͤ͞ɺJSONϑΝΠϧΛμ΢ϯϩʔυ͠·͢ɻ ޙͰ࢖͍·͢ʂ

  9. How to ̍. go get $ go get github.com/yyoshiki41/go-gmail-drafts $

    cd $GOPATH/src/github.com/yyoshiki41/go-gmail-drafts ̎. μ΢ϯϩʔυͨ͠jsonϑΝΠϧΛઃஔɻ $ mv ~/Downloads/clientid.json config/clientsecret.json
  10. ̏. ॳճ࣮ߦ࣌͸OAuth௨͠ɺΞΫηετʔΫϯΛऔಘ͢Δɻ $ go run savetoken/main.go ϒϥ΢β͕ࣗಈͰ։͖ɺgoogleΞΧ΢ϯτͷೝূΛ͠·͢ɻ ࣍ʹɺGmailͰͷΞΫγϣϯΛڐՄ͠·͢ɻ

  11. ̑ɽςΩετϘοΫεʹදࣔ͞ΕͨcodeΛίϐʔ͢Δɻ λʔϛφϧʹ໭ͬͯɺtokenϑΝΠϧΛੜ੒ɻ ίϐʔͨ͠ίʔυΛషΓ෇͚Δɻ

  12. ̑. Լॻ͖ςϯϓϨΛ࡞੒ɻ { "to": "xxx@gmail.com", "subject": "Daily Reports", "message": "Hello!\nThis

    is a draft." }
  13. ̒. go runͰ࣮ߦɻ $ go run main.go

  14. ऴΘΓʹ go buildͰόΠφϦԽͯ͠ɺ cron౳ʹొ࿥͢Ε͹ຖ೔Լॻ͖࡞੒౳ʹ࢖͑·͢ʂ