File size: 6,378 Bytes
c9329ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from taipy import Gui
from taipy.gui import navigate # type: ignore
import taipy.gui.builder as tgb
from functi import *
from Source_manage import source_page, menu_page
from Post_management import Post_manag
from flask import Flask, request
from urllib.parse import urlparse, parse_qs
import time
import logging
logging.getLogger('requests_oauthlib').setLevel(logging.DEBUG)


import requests

def obtenir_access_token(code, client_id, client_secret, redirect_uri):
    url = "https://www.linkedin.com/oauth/v2/accessToken"
    headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    }
    data = {
        "grant_type": "authorization_code",
        "code": code,
        "redirect_uri": redirect_uri,
        "client_id": client_id,
        "client_secret": client_secret
    }

    response = requests.post(url, headers=headers, data=data)
    response.raise_for_status()  # lève une exception si le statut HTTP n’est pas 2xx
    return response.json()


# Presentation text
presentation_text = """
Découvrez Lin votre assistant community manager sur Linkedin
"""





def on_navigate(state, page_name: str):
    if page_name == "callback":
        raw = request.environ.get("REQUEST_URI", "")
      
    
        full = request.scheme + "://" + request.host + raw
        state.authorization_url = full
        state.authorization_url = state.authorization_url.replace("/taipy-jsx", "")
        state.authorization_url = state.authorization_url.rsplit("&v", 1)[0]
        state.authorization_url = state.authorization_url.replace("http://", "https://")
        print(state.authorization_url,flush = True)
        parsed = urlparse(state.authorization_url)

        # 2. Extrait les paramètres dans un dict (valeur encapsulée dans une liste)
        params = parse_qs(parsed.query)
        
        # 3. Récupère la valeur de state
        state_value = params.get("state", [""])[0]
        code = params.get("code", [""])[0]
        print(state_value,flush = True)
        print(state.states,flush = True)
        print(code,flush = True)
        if state_value.endswith('?'):
            state_value = state_value[:-1]
        else:
            state_value = state_value
        
        if state_value == state.states :
            if state.Linked_social_network == "Linkedin" :
                response = obtenir_access_token(code, state.client_id, state.client_secret, state.redirect_url)
                
                state.token = response["access_token"]
                
                
                url = "https://api.linkedin.com/v2/userinfo"
                headers = {
                    "Authorization": f"Bearer {state.token}"
                }
                
                response_open = requests.get(url, headers=headers)
                response_open =response_open.json()
                db_manager.add_token_network(state.token,state.Linked_social_network,state.Linked_account_name,state.user_inf.user.id,response_open)
                navigate(state,"Source_Management" )

                
                print(state.token,flush = True)
            else :
                print("mkjgfzmfvhsmehk")


            
    return page_name








    

with tgb.Page(class_name="bodyp") as callb:
    tgb.text("attendez un moment")

# Create pages
with tgb.Page(class_name="bodyp") as page_appart:
    
    # Show login/register forms if not logged in
    with tgb.part(render="{not is_logged_in}"): # type: ignore
        with tgb.part(class_name="presentation", height="100vh"):
            tgb.text("Lin", class_name="App_name")
            
            
            with tgb.part(class_name="presentation_text"):
                tgb.text(presentation_text, id="presentation_text_t")
                with tgb.part(class_name="auth-container"):
                    
                    
                    # Login form (default)
                    with tgb.part(render="{not show_register}"):  # type: ignore
                        tgb.text("Login to Lin")
                        tgb.input("{login_email}", label="Email", type="email",change_delay = -1,action_on_blur = True)
                        tgb.input("{login_password}", label="Password", password=True,change_delay = -1,action_on_blur = True)
                        tgb.button("Login", on_action=on_login )
                        tgb.button("Need an account? Register", on_action=toggle_register)
                    
                    # Register form
                    with tgb.part(render="{show_register}"): # type: ignore
                        tgb.text("Register for Lin", )
                        tgb.input("{register_email}", label="Email", type="email",change_delay = -1,action_on_blur = True)
                        tgb.input("{register_password}", label="Password", password=True,change_delay = -1,action_on_blur = True)
                        tgb.input("{confirm_password}", label="Confirm Password", password=True,change_delay = -1,action_on_blur = True)
                        tgb.button("Register", on_action=on_register, class_name="auth-button")
                        tgb.button("Already have an account? Login", on_action=toggle_register, )
                    
                    # Message display
                    with tgb.part(render="{message}"): # type: ignore
                        tgb.text("{message}", class_name="message")
    
    # Show main content if logged in
    with tgb.part(render="{is_logged_in}"): # type: ignore
        with tgb.part(class_name="presentation", height="100vh"):
            tgb.text("Lin", class_name="App_name")
            tgb.text(f"Welcome, {'{current_user}'}!", class_name="welcome-message")
            tgb.button("Logout", on_action=on_logout, class_name="logout-button")
            
            with tgb.part(class_name="presentation_text"):
                tgb.text(presentation_text, id="presentation_text_t")

# Define pages
pages = {
    "Accueil": page_appart,
    "/": menu_page,
    "Source_Management" : source_page,
    "callback" : callb,
    "Post" : Post_manag
}

stylekit = {
  "color_primary" : "#910029",
  
}

if __name__ == "__main__":
    planning()

    gui = Gui(pages=pages)
    gui.run(
        debug=True,
        port=7860,host = "0.0.0.0",
        stylekit=stylekit,
        title="Lin",
        dark_mode=False,
        use_reloader=True
    )