14
14
// See the License for the specific language governing permissions and
15
15
// limitations under the License.
16
16
17
- package facebooklib
17
+ package fblib
18
18
19
19
import (
20
20
"bytes"
21
+ "encoding/json"
22
+ "errors"
21
23
"fmt"
22
- "http"
23
24
"io"
24
25
"io/ioutil"
25
- "json"
26
- "os"
26
+ "net/http"
27
27
"strconv"
28
28
29
+ "net/url"
29
30
"time"
30
- "url"
31
31
)
32
32
33
33
var (
34
- ErrOAuth = os .NewError ("OAuth authorization failure" )
35
- )
34
+ ErrOAuth = errors .New ("OAuth authorization failure" )
35
+ )
36
+
36
37
const (
37
- tokenRequestURL = "https://www.facebook .com/dialog/oauth" // request token endpoint
38
+ tokenRequestURL = "https://www.faceook .com/dialog/oauth" // request token endpoint
38
39
accessTokenURL = "https://graph.facebook.com/oauth/access_token" // access token endpoint
39
40
40
41
apiURL = "https://graph.facebook.com"
@@ -53,15 +54,15 @@ type TempToken struct {
53
54
}
54
55
55
56
func nonce () string {
56
- s := time .Nanoseconds ()
57
- return strconv .Itoa64 ( s )
57
+ s := time .Now ()
58
+ return strconv .FormatInt ( s . Unix (), 10 )
58
59
}
59
60
60
61
func NewFacebookClient (key , secret string ) * FacebookClient {
61
62
return & FacebookClient {APIKey : key ,
62
63
AppSecret : secret ,
63
64
Transport : http .DefaultTransport }
64
- }
65
+ }
65
66
66
67
func (fc * FacebookClient ) AuthURL (redirectURI , scope string ) string {
67
68
params := make (url.Values )
@@ -73,9 +74,7 @@ func (fc *FacebookClient) AuthURL(redirectURI, scope string) string {
73
74
return fmt .Sprintf ("%s?%s" , tokenRequestURL , params .Encode ())
74
75
}
75
76
76
-
77
-
78
- func (fc * FacebookClient ) RequestAccessToken (code , redirectURI string ) os.Error {
77
+ func (fc * FacebookClient ) RequestAccessToken (code , redirectURI string ) error {
79
78
var body io.Reader
80
79
body = bytes .NewBuffer ([]byte ("" ))
81
80
params := make (url.Values )
@@ -108,7 +107,7 @@ func (fc *FacebookClient) RequestAccessToken(code, redirectURI string) os.Error
108
107
return nil
109
108
}
110
109
111
- func (fc * FacebookClient ) parseError (respBody []byte ) os. Error {
110
+ func (fc * FacebookClient ) parseError (respBody []byte ) error {
112
111
var buf map [string ]interface {}
113
112
json .Unmarshal (respBody , & buf )
114
113
errorMap := buf ["error" ]
@@ -120,14 +119,14 @@ func (fc *FacebookClient) parseError(respBody []byte) os.Error {
120
119
if kind == "OAuthException" {
121
120
return ErrOAuth
122
121
} else {
123
- return os . NewError (error ["message" ].(string ))
122
+ return errors . New (error ["message" ].(string ))
124
123
}
125
124
}
126
125
}
127
- return os . NewError ("Unknown error" )
126
+ return errors . New ("Unknown error" )
128
127
}
129
128
130
- func (fc * FacebookClient ) GetUser (id string ) (* User , os. Error ) {
129
+ func (fc * FacebookClient ) GetUser (id string ) (* User , error ) {
131
130
u := make (url.Values )
132
131
u .Set ("access_token" , fc .AccessToken )
133
132
body := bytes .NewBuffer ([]byte (u .Encode ()))
@@ -154,7 +153,7 @@ func (fc *FacebookClient) GetUser(id string) (*User, os.Error) {
154
153
155
154
// Performs API call based on httpMethod
156
155
// 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 ) {
158
157
body := bytes .NewBuffer ([]byte (params .Encode ()))
159
158
cmdStr := fmt .Sprintf ("%s/%s?access_token=%s" , apiURL , endpoint , fc .AccessToken )
160
159
if httpMethod == "GET" {
@@ -179,25 +178,27 @@ func (fc *FacebookClient) Call(httpMethod, endpoint string, params url.Values) (
179
178
return respBody , nil
180
179
}
181
180
182
- func (fc * FacebookClient ) User (id string ) (* User , os. Error ) {
181
+ func (fc * FacebookClient ) User (id string ) (* User , error ) {
183
182
u := new (url.Values )
184
183
resp , err := fc .Call ("GET" , id , * u )
185
184
if err != nil {
186
185
return nil , err
187
186
}
188
187
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 )
192
193
user .Client = fc
193
194
return user , nil
194
195
}
195
196
196
- func (fc * FacebookClient ) CurrentUser () (* User , os. Error ) {
197
+ func (fc * FacebookClient ) CurrentUser () (* User , error ) {
197
198
return fc .User ("me" )
198
199
}
199
200
200
- func (fc * FacebookClient ) PostLink (message , link string ) os. Error {
201
+ func (fc * FacebookClient ) PostLink (message , link string ) error {
201
202
u := make (url.Values )
202
203
u .Add ("message" , message )
203
204
u .Add ("link" , link )
@@ -206,9 +207,9 @@ func (fc *FacebookClient) PostLink(message, link string) os.Error {
206
207
return err
207
208
}
208
209
209
- func (fc * FacebookClient ) PostStatus (message string ) os. Error {
210
+ func (fc * FacebookClient ) PostStatus (message string ) error {
210
211
u := make (url.Values )
211
212
u .Add ("message" , message )
212
213
_ , err := fc .Call ("POST" , "me/feed" , u )
213
214
return err
214
- }
215
+ }
0 commit comments