Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some Session methods don't work with http client created by golang.org/x/oauth2 #40

Closed
VojtechVitek opened this issue Jul 29, 2015 · 9 comments

Comments

@VojtechVitek
Copy link

How can I use this pkg with golang.org/x/oauth2 properly?

package main

import (
    "golang.org/x/oauth2"
    oauth2fb "golang.org/x/oauth2/facebook"

    "github.com/huandu/facebook"
)

func main() {

    conf := &oauth2.Config{
        ClientID:     "AppId",
        ClientSecret: "AppSecret",
        RedirectURL:  "CallbackURL",
        Scopes:       []string{"user_location"},
        Endpoint:     oauth2fb.Endpoint,
    }

    token, err := conf.Exchange(oauth2.NoContext, "code")

    client := conf.Client(oauth2.NoContext, tok)

    // Now, I want to pass the above client
    // (that already provides the AccessTokens)
    // into the facebook Session.
    // 
    // ... I tried:

    api := &facebook.Session{
        HttpClient: client,
        Version: "2.2",
    }

    err := api.Validate()  // <= returns "access token is not set" error
}

The question is... how do I create new Session with my own HttpClient and without any AccessToken (as my client's Transport already handles that)?

@VojtechVitek
Copy link
Author

I ended up with this:

app := facebook.New("AppId", "AppSecret")
api := app.Session("token")
api.Version = "v2.2"
api.HttpClient = client  // Client coming from golang.org/x/oauth2.

The only thing I'm not satisfied with is that this pkg forces me to provide AppId, AppSecret and Token. Which is what golang.org/x/oauth2 client already handles for me.

Any suggestions? :)

@huandu
Copy link
Owner

huandu commented Jul 30, 2015

You can use Session#SetAccessToken to set access token without having to create an App.

@VojtechVitek
Copy link
Author

Here's what I'm saying:
I don't want to set AccessToken at all, as the golang.org/x/oauth2 client's Transport already handles that for me :)

So, is it possible to use the API directly without setting AppId, AppSecret and AccessToken? Seems like most of the methods fail on checks like

facebook/session.go

Lines 229 to 232 in ff27e4b

if session.accessToken == "" {
err = fmt.Errorf("access token is not set.")
return
}
.

Thanks for your feedback:)

@VojtechVitek
Copy link
Author

I guess I could use session.Get("/me", Params{"fields": "id"}) directly, I will give it a shot.

@VojtechVitek
Copy link
Author

Given

    api := &facebook.Session{
        Version:    "v2.4",
        HttpClient: client,
    }

I can't use the following methods, as they return errors because of missing Session ID / AccessToken:

    err := api.Validate()

    id, err := api.User()

But the following works :-)

    res, err := api.Get("/me/accounts", getFbParams(url.Values{}))

@huandu
Copy link
Owner

huandu commented Jul 30, 2015

OK. I get your point. I think it's reasonable to by-pass some access token checks when Session#HttpClient is set.

@VojtechVitek
Copy link
Author

Yea, that'd do it. Thanks!

@huandu
Copy link
Owner

huandu commented Jul 30, 2015

Please try latest code and let me know whether it works for you now.

@VojtechVitek
Copy link
Author

That was fast, wow.. And it works now!

@huandu Thank you very much for such a fast resolution and for this project in general. Cheers.

@huandu huandu changed the title golang.org/x/oauth2 Some Session methods don't work with http client created by golang.org/x/oauth2 Jul 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants