Giter VIP home page Giter VIP logo

Comments (5)

lipanski avatar lipanski commented on June 2, 2024

hi @mayah

you can still set the response_type even with the current implementation:

let mut authorize_url = config.authorize_url(String::new());
authorize_url.query_pairs_mut().append_pair("response_type", "code");

this should happen somewhere before you call config.exchange_code(code).

my issue #8 is about improving the interface, but after #7 I got the Google flow working.

from oauth2-rs.

lipanski avatar lipanski commented on June 2, 2024

here's the entire code (in this case for Google Calendar):

    let mut config = Oauth2Config::new(
        "YOUR_CLIENT_ID",
        "YOUR_CLIENT_SECRET",
        "https://accounts.google.com/o/oauth2/v2/auth",
        "https://www.googleapis.com/oauth2/v3/token"
    );

    // Point to your redirect URL
    config.redirect_url = "http://localhost:8080".to_string();

    config.scopes = Vec::new();
    config.scopes.push("https://www.googleapis.com/auth/calendar".to_string());

    let mut authorize_url = config.authorize_url(String::new());
    authorize_url.query_pairs_mut().append_pair("response_type", "code");

    let mut code = String::new();

    // This is my own-the-fly redirect URL server: Ignore this part if you've got this handled somewhere else
    let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
    for stream in listener.incoming() {
        match stream {
            Ok(stream) => {
                let mut reader = BufReader::new(stream);

                let mut request_line = String::new();
                reader.read_line(&mut request_line).unwrap();

                let redirect_url = request_line.split_whitespace().nth(1).unwrap();
                let url = Url::parse(&("http://localhost".to_string() + redirect_url)).unwrap();

                let code_pair = url.query_pairs().find(|pair| {
                    let &(ref key, _) = pair;
                    key == "code"
                }).unwrap();

                let (_, value) = code_pair;
                code = value.into_owned();

                break;
            }
            Err(e) => {}
        }
    };

    let token = config.exchange_code(code).unwrap();

from oauth2-rs.

mayah avatar mayah commented on June 2, 2024

yeah, I'm ok with adding query params after taking authorize_url, however, I don't think it's intuitive. If it's ok, then it might be also ok if we construct authorize_url without this kind of library, right?

Anyway, it looks you're working on enhancing the api interface, I'll wait for your work. (or, I'm ok to make a PR to add an interface.)

from oauth2-rs.

lipanski avatar lipanski commented on June 2, 2024

@mayah I've closed that monster ticket and just released 1.0.0 => have a look if it's still of interest. the documentation is available here.

from oauth2-rs.

mayah avatar mayah commented on June 2, 2024

Hi, thanks for the info! I'll check it.

from oauth2-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.