Skip to content

Commit 285ba45

Browse files
committed
Updated to Go1
1 parent 4862635 commit 285ba45

File tree

5 files changed

+44
-57
lines changed

5 files changed

+44
-57
lines changed

Makefile

-9
This file was deleted.

README.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Installing
88

99
Best way to isntall this package is by running goinstall:
1010

11-
goinstall github.com/robteix/fblib
11+
go get github.com/robteix/fblib
1212

1313
And then you can import it in your code like this:
1414

@@ -18,11 +18,6 @@ And then you can import it in your code like this:
1818
...
1919
)
2020

21-
And of course, if you want to do it manually, just check the code,
22-
cd to the project's root dir and do a:
23-
24-
gomake install
25-
2621
License
2722
-------
2823

facebook.go

+28-27
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,28 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17-
package facebooklib
17+
package fblib
1818

1919
import (
2020
"bytes"
21+
"encoding/json"
22+
"errors"
2123
"fmt"
22-
"http"
2324
"io"
2425
"io/ioutil"
25-
"json"
26-
"os"
26+
"net/http"
2727
"strconv"
2828

29+
"net/url"
2930
"time"
30-
"url"
3131
)
3232

3333
var (
34-
ErrOAuth = os.NewError("OAuth authorization failure")
35-
)
34+
ErrOAuth = errors.New("OAuth authorization failure")
35+
)
36+
3637
const (
37-
tokenRequestURL = "https://www.facebook.com/dialog/oauth" // request token endpoint
38+
tokenRequestURL = "https://www.faceook.com/dialog/oauth" // request token endpoint
3839
accessTokenURL = "https://graph.facebook.com/oauth/access_token" // access token endpoint
3940

4041
apiURL = "https://graph.facebook.com"
@@ -53,15 +54,15 @@ type TempToken struct {
5354
}
5455

5556
func nonce() string {
56-
s := time.Nanoseconds()
57-
return strconv.Itoa64(s)
57+
s := time.Now()
58+
return strconv.FormatInt(s.Unix(), 10)
5859
}
5960

6061
func NewFacebookClient(key, secret string) *FacebookClient {
6162
return &FacebookClient{APIKey: key,
6263
AppSecret: secret,
6364
Transport: http.DefaultTransport}
64-
}
65+
}
6566

6667
func (fc *FacebookClient) AuthURL(redirectURI, scope string) string {
6768
params := make(url.Values)
@@ -73,9 +74,7 @@ func (fc *FacebookClient) AuthURL(redirectURI, scope string) string {
7374
return fmt.Sprintf("%s?%s", tokenRequestURL, params.Encode())
7475
}
7576

76-
77-
78-
func (fc *FacebookClient) RequestAccessToken(code, redirectURI string) os.Error {
77+
func (fc *FacebookClient) RequestAccessToken(code, redirectURI string) error {
7978
var body io.Reader
8079
body = bytes.NewBuffer([]byte(""))
8180
params := make(url.Values)
@@ -108,7 +107,7 @@ func (fc *FacebookClient) RequestAccessToken(code, redirectURI string) os.Error
108107
return nil
109108
}
110109

111-
func (fc *FacebookClient) parseError(respBody []byte) os.Error {
110+
func (fc *FacebookClient) parseError(respBody []byte) error {
112111
var buf map[string]interface{}
113112
json.Unmarshal(respBody, &buf)
114113
errorMap := buf["error"]
@@ -120,14 +119,14 @@ func (fc *FacebookClient) parseError(respBody []byte) os.Error {
120119
if kind == "OAuthException" {
121120
return ErrOAuth
122121
} else {
123-
return os.NewError(error["message"].(string))
122+
return errors.New(error["message"].(string))
124123
}
125124
}
126125
}
127-
return os.NewError("Unknown error")
126+
return errors.New("Unknown error")
128127
}
129128

130-
func (fc *FacebookClient) GetUser(id string) (*User, os.Error) {
129+
func (fc *FacebookClient) GetUser(id string) (*User, error) {
131130
u := make(url.Values)
132131
u.Set("access_token", fc.AccessToken)
133132
body := bytes.NewBuffer([]byte(u.Encode()))
@@ -154,7 +153,7 @@ func (fc *FacebookClient) GetUser(id string) (*User, os.Error) {
154153

155154
// Performs API call based on httpMethod
156155
// returns the response body as string and error/nil
157-
func (fc *FacebookClient) Call(httpMethod, endpoint string, params url.Values) ([]byte, os.Error) {
156+
func (fc *FacebookClient) Call(httpMethod, endpoint string, params url.Values) ([]byte, error) {
158157
body := bytes.NewBuffer([]byte(params.Encode()))
159158
cmdStr := fmt.Sprintf("%s/%s?access_token=%s", apiURL, endpoint, fc.AccessToken)
160159
if httpMethod == "GET" {
@@ -179,25 +178,27 @@ func (fc *FacebookClient) Call(httpMethod, endpoint string, params url.Values) (
179178
return respBody, nil
180179
}
181180

182-
func (fc *FacebookClient) User(id string) (*User, os.Error) {
181+
func (fc *FacebookClient) User(id string) (*User, error) {
183182
u := new(url.Values)
184183
resp, err := fc.Call("GET", id, *u)
185184
if err != nil {
186185
return nil, err
187186
}
188187
user := new(User)
189-
if err = json.Unmarshal(resp, user); err != nil {
190-
return nil, err
191-
}
188+
189+
//if err = json.Unmarshal(resp, user); err != nil {
190+
// return nil, os.NewError(fmt.Sprintf("fc.User error -> %s (resp body: '%s')", err, resp))
191+
//}
192+
json.Unmarshal(resp, user)
192193
user.Client = fc
193194
return user, nil
194195
}
195196

196-
func (fc *FacebookClient) CurrentUser() (*User, os.Error) {
197+
func (fc *FacebookClient) CurrentUser() (*User, error) {
197198
return fc.User("me")
198199
}
199200

200-
func (fc *FacebookClient) PostLink(message, link string) os.Error {
201+
func (fc *FacebookClient) PostLink(message, link string) error {
201202
u := make(url.Values)
202203
u.Add("message", message)
203204
u.Add("link", link)
@@ -206,9 +207,9 @@ func (fc *FacebookClient) PostLink(message, link string) os.Error {
206207
return err
207208
}
208209

209-
func (fc *FacebookClient) PostStatus(message string) os.Error {
210+
func (fc *FacebookClient) PostStatus(message string) error {
210211
u := make(url.Values)
211212
u.Add("message", message)
212213
_, err := fc.Call("POST", "me/feed", u)
213214
return err
214-
}
215+
}

types.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// twitterlib - A simple, fully oauth-authenticated Twitter library
1+
// fblib - A simple, fully oauth-authenticated Twitter library
22

33
// Copyright (c) 2011, Roberto Teixeira <[email protected]>
44
//
@@ -14,7 +14,7 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17-
package facebooklib
17+
package fblib
1818

1919
// User object. See http://developers.facebook.com/docs/reference/api/user/
2020
type User struct {

user.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
1-
package facebooklib
1+
package fblib
22

33
import (
4-
"os"
5-
"url"
6-
)
4+
"errors"
5+
"net/url"
6+
)
77

88
var (
9-
UserNotConnectedError = os.NewError("User Not Connected")
10-
)
9+
UserNotConnectedError = errors.New("User Not Connected")
10+
)
1111

1212
// Useful if all you want is to have an Id to perform actions
1313
// This prevents having to query a user object from FB.
1414
func NewUser(Id string, fc *FacebookClient) *User {
1515
return &User{Id: Id, Client: fc}
1616
}
1717

18-
func (user *User) PostStatus(message string) os.Error {
18+
func (user *User) PostStatus(message string) error {
1919
if user.Client == nil {
2020
return UserNotConnectedError
2121
}
2222
if message == "" {
23-
return os.NewError("Missing message parameter")
23+
return errors.New("Missing message parameter")
2424
}
2525
u := make(url.Values)
2626
u.Set("message", message)
27-
_, err := user.Client.Call("POST", user.Id + "/feed", u)
27+
_, err := user.Client.Call("POST", user.Id+"/feed", u)
2828
return err
2929
}
3030

31-
func (user *User) PostLink(link, message string) os.Error {
31+
func (user *User) PostLink(link, message string) error {
3232
if user.Client == nil {
3333
return UserNotConnectedError
3434
}
3535
if link == "" {
36-
return os.NewError("Missing link parameter")
36+
return errors.New("Missing link parameter")
3737
}
3838
u := make(url.Values)
3939
if message != "" {
4040
u.Set("message", message)
4141
}
4242
u.Set("link", link)
43-
_, err := user.Client.Call("POST", user.Id + "/links", u)
43+
_, err := user.Client.Call("POST", user.Id+"/links", u)
4444
return err
45-
}
45+
}

0 commit comments

Comments
 (0)