slack_chat_api/
oauth.rs

1use crate::Client;
2use crate::ClientResult;
3
4pub struct Oauth {
5    pub client: Client,
6}
7
8impl Oauth {
9    #[doc(hidden)]
10    pub fn new(client: Client) -> Self {
11        Oauth { client }
12    }
13
14    /**
15     * This function performs a `GET` to the `/oauth.access` endpoint.
16     *
17     * Exchanges a temporary OAuth verifier code for an access token.
18     *
19     * FROM: <https://api.slack.com/methods/oauth.access>
20     *
21     * **Parameters:**
22     *
23     * * `client_id: &str` -- Issued when you created your application.
24     * * `client_secret: &str` -- Issued when you created your application.
25     * * `code: &str` -- The `code` param returned via the OAuth callback.
26     * * `redirect_uri: &str` -- This must match the originally submitted URI (if one was sent).
27     * * `single_channel: bool` -- Request the user to add your app only to a single channel. Only valid with a [legacy workspace app](https://api.slack.com/legacy-workspace-apps).
28     */
29    pub async fn access(
30        &self,
31        client_id: &str,
32        client_secret: &str,
33        code: &str,
34        redirect_uri: &str,
35        single_channel: bool,
36    ) -> ClientResult<crate::Response<crate::types::DndEndSchema>> {
37        let mut query_args: Vec<(String, String)> = Default::default();
38        if !client_id.is_empty() {
39            query_args.push(("client_id".to_string(), client_id.to_string()));
40        }
41        if !client_secret.is_empty() {
42            query_args.push(("client_secret".to_string(), client_secret.to_string()));
43        }
44        if !code.is_empty() {
45            query_args.push(("code".to_string(), code.to_string()));
46        }
47        if !redirect_uri.is_empty() {
48            query_args.push(("redirect_uri".to_string(), redirect_uri.to_string()));
49        }
50        if single_channel {
51            query_args.push(("single_channel".to_string(), single_channel.to_string()));
52        }
53        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
54        let url = self.client.url(&format!("/oauth.access?{}", query_), None);
55        self.client
56            .get(
57                &url,
58                crate::Message {
59                    body: None,
60                    content_type: None,
61                },
62            )
63            .await
64    }
65    /**
66     * This function performs a `GET` to the `/oauth.token` endpoint.
67     *
68     * Exchanges a temporary OAuth verifier code for a workspace token.
69     *
70     * FROM: <https://api.slack.com/methods/oauth.token>
71     *
72     * **Parameters:**
73     *
74     * * `client_id: &str` -- Issued when you created your application.
75     * * `client_secret: &str` -- Issued when you created your application.
76     * * `code: &str` -- The `code` param returned via the OAuth callback.
77     * * `redirect_uri: &str` -- This must match the originally submitted URI (if one was sent).
78     * * `single_channel: bool` -- Request the user to add your app only to a single channel.
79     */
80    pub async fn token(
81        &self,
82        client_id: &str,
83        client_secret: &str,
84        code: &str,
85        redirect_uri: &str,
86        single_channel: bool,
87    ) -> ClientResult<crate::Response<crate::types::DndEndSchema>> {
88        let mut query_args: Vec<(String, String)> = Default::default();
89        if !client_id.is_empty() {
90            query_args.push(("client_id".to_string(), client_id.to_string()));
91        }
92        if !client_secret.is_empty() {
93            query_args.push(("client_secret".to_string(), client_secret.to_string()));
94        }
95        if !code.is_empty() {
96            query_args.push(("code".to_string(), code.to_string()));
97        }
98        if !redirect_uri.is_empty() {
99            query_args.push(("redirect_uri".to_string(), redirect_uri.to_string()));
100        }
101        if single_channel {
102            query_args.push(("single_channel".to_string(), single_channel.to_string()));
103        }
104        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
105        let url = self.client.url(&format!("/oauth.token?{}", query_), None);
106        self.client
107            .get(
108                &url,
109                crate::Message {
110                    body: None,
111                    content_type: None,
112                },
113            )
114            .await
115    }
116}