-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
simone simonella
committed
Apr 10, 2017
1 parent
4c8e91b
commit 445bbe2
Showing
2 changed files
with
158 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
# Go | ||
|
||
Some of my projects developed with Golang | ||
|
||
### Colours | ||
|
||
Prende in input da linea di comando un'immagine png/jpeg e, dopo un'analisi pixel per pixel ritorna gli n colori più usati | ||
I use this repository to track my growth | ||
|
||
### **colours** Colours | ||
#### Necessary setup | ||
No additional packages required | ||
#### Short Workflow Description | ||
Accepts an image as parameter (png or jpeg), and gives back the most frequent colours | ||
#### Short Algorithm Description | ||
Executes a pixel by pixel scan of the image, uses a hash function to speed up the process | ||
|
||
### **mailsender** GMail Api Integration with Mail Sending Example | ||
#### Necessary setup | ||
Follow [this](https://developers.google.com/gmail/api/quickstart/go "GMail Api") Google quickstart to setup your mail | ||
#### Short Workflow Description | ||
Uses the specified (sender) account to send a mail to the specified (receiver) account. Needs to be authorized! | ||
#### Short Algorithm Description | ||
Search for client_secret.json file, request a token to the google mail api and then send the mail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"os/user" | ||
"path/filepath" | ||
"golang.org/x/net/context" | ||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/google" | ||
"google.golang.org/api/gmail/v1" | ||
"encoding/base64" | ||
) | ||
|
||
// getClient uses a Context and Config to retrieve a Token | ||
// then generate a Client. It returns the generated Client. | ||
func getClient(ctx context.Context, config *oauth2.Config) *http.Client { | ||
cacheFile, err := tokenCacheFile() | ||
if err != nil { | ||
log.Fatalf("Unable to get path to cached credential file. %v", err) | ||
} | ||
tok, err := tokenFromFile(cacheFile) | ||
if err != nil { | ||
tok = getTokenFromWeb(config) | ||
saveToken(cacheFile, tok) | ||
} | ||
return config.Client(ctx, tok) | ||
} | ||
|
||
// getTokenFromWeb uses Config to request a Token. | ||
// It returns the retrieved Token. | ||
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token { | ||
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline) | ||
fmt.Printf("Go to the following link in your browser then type the " + | ||
"authorization code: \n%v\n", authURL) | ||
|
||
var code string | ||
if _, err := fmt.Scan(&code); err != nil { | ||
log.Fatalf("Unable to read authorization code %v", err) | ||
} | ||
|
||
tok, err := config.Exchange(oauth2.NoContext, code) | ||
if err != nil { | ||
log.Fatalf("Unable to retrieve token from web %v", err) | ||
} | ||
return tok | ||
} | ||
|
||
// tokenCacheFile generates credential file path/filename. | ||
// It returns the generated credential path/filename. | ||
func tokenCacheFile() (string, error) { | ||
usr, err := user.Current() | ||
if err != nil { | ||
return "", err | ||
} | ||
tokenCacheDir := filepath.Join(usr.HomeDir, ".credentials") | ||
os.MkdirAll(tokenCacheDir, 0700) | ||
return filepath.Join(tokenCacheDir, | ||
url.QueryEscape("gmail-go-quickstart.json")), err | ||
} | ||
|
||
// tokenFromFile retrieves a Token from a given file path. | ||
// It returns the retrieved Token and any read error encountered. | ||
func tokenFromFile(file string) (*oauth2.Token, error) { | ||
f, err := os.Open(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
t := &oauth2.Token{} | ||
err = json.NewDecoder(f).Decode(t) | ||
defer f.Close() | ||
return t, err | ||
} | ||
|
||
// saveToken uses a file path to create a file and store the | ||
// token in it. | ||
func saveToken(file string, token *oauth2.Token) { | ||
fmt.Printf("Saving credential file to: %s\n", file) | ||
f, err := os.OpenFile(file, os.O_RDWR | os.O_CREATE | os.O_TRUNC, 0600) | ||
if err != nil { | ||
log.Fatalf("Unable to cache oauth token: %v", err) | ||
} | ||
defer f.Close() | ||
json.NewEncoder(f).Encode(token) | ||
} | ||
|
||
// --- MAIN --- | ||
|
||
func main() { | ||
ctx := context.Background() | ||
|
||
b, err := ioutil.ReadFile("client_secret.json") | ||
if err != nil { | ||
log.Fatalf("Unable to read client secret file: %v", err) | ||
} | ||
|
||
// If modifying these scopes, delete your previously saved credentials | ||
// at ~/.credentials/gmail-go-quickstart.json | ||
config, err := google.ConfigFromJSON(b, gmail.GmailSendScope) | ||
if err != nil { | ||
log.Fatalf("Unable to parse client secret file to config: %v", err) | ||
} | ||
client := getClient(ctx, config) | ||
|
||
srv, err := gmail.New(client) | ||
if err != nil { | ||
log.Fatalf("Unable to retrieve gmail Client %v", err) | ||
} | ||
|
||
// New message for our gmail service to send | ||
var message gmail.Message | ||
|
||
sender := "[email protected]" | ||
reciever := "[email protected]" | ||
subject := "Sample Subject" | ||
msg := "Sample Message" | ||
|
||
// Compose the message | ||
messageStr := []byte( | ||
"From: " + sender + "\r\n" + | ||
"To: " + reciever + "\r\n" + | ||
"Subject: " + subject + "\r\n\r\n" + | ||
msg) | ||
|
||
// Place messageStr into message.Raw in base64 encoded format | ||
message.Raw = base64.URLEncoding.EncodeToString(messageStr) | ||
|
||
// Send the message | ||
_, err = srv.Users.Messages.Send("me", &message).Do() | ||
if err != nil { | ||
log.Printf("Error: %v", err) | ||
} else { | ||
fmt.Println("Message sent!") | ||
} | ||
} |