-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
65 lines (54 loc) · 1.45 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"log"
"os"
"os/user"
"github.com/maxchehab/geddit"
survey "gopkg.in/AlecAivazis/survey.v1"
)
// GetSession creates geddit session using OAuth
func GetSession(username string, password string) (session *geddit.OAuthSession, err error) {
// session, err := geddit.NewOAuthSession(
// "EV4dAIStivu3XA",
// "KnLTb7s6CrP9KlX_vhLwQ27VMW4",
// "redditfs",
// "http://maxchehab.com",
// 77346c3e708a
// )
name, id, secret, read, err := GetOauthCredentials()
if err != nil {
return
}
session, err = geddit.NewOAuthSession(
id,
secret,
name,
"http://maxchehab.com",
)
if err != nil {
log.Fatal(err)
}
// Create new auth token for confidential clients (personal scripts/apps).
err = session.LoginAuth(username, password)
if err != nil {
if err.Error() == "oauth2: cannot fetch token: 401 Unauthorized\nResponse: {\"message\": \"Unauthorized\", \"error\": 401}" && read {
removeFile := false
removeFilePrompt := &survey.Confirm{
Message: "The OAUTH credentials provided for your authorized reddit application were incorrect. Would you like to change your credentials?",
}
survey.AskOne(removeFilePrompt, &removeFile, nil)
if removeFile {
usr, err := user.Current()
if err != nil {
return nil, err
}
credentialPath := usr.HomeDir + "/.redditfscredentials"
os.Remove(credentialPath)
return GetSession(username, password)
}
} else {
return nil, err
}
}
return
}