3

내 북마크 확장에 대한 사용자 인증을 개발 중입니다. 확장 프로그램은 Google App Engine (Python) 백엔드 http://ting-1.appspot.com/에서 작동합니다.데이터를 Chrome 확장 프로그램으로 보내려면 어떻게해야하나요?

확장 프로그램이 처음 실행될 때 background.htmloptions.html을 새 탭에서 열어 사용자가 전자 메일 주소를 입력합니다. 사용자가를 클릭하면

class AuthSender(webapp.RequestHandler): 
    def post(self): 
     new_user = self.request.get("extension_user") 
     mail.send_mail(
     sender="Ting <[email protected]>", 
     to=new_user, 
     subject="Ting Bookmarking: confirm your email", 
     body="""click on the link below to confirm your email: 
<a href="http://ting-1.appspot.com/authHandler"></a> 

Happy Bookmarking!""") 

:이 같은 응용 프로그램 (아직 시도)에 이메일 주소를 보내 응용 프로그램에서

in options.html 
--------------- 

var formData = new FormData(); 

formData.append("extension_user", userEmail); 

var xhr = new XMLHttpRequest(); 
xhr.open("POST", "http://ting-1.appspot.com/authsender", true); 
xhr.send(formData); 

, 핸들러 AuthSender는 사용자에게 확인 이메일을 전송 응용 프로그램의 이메일 AuthReceive 처리기의 링크가 요청을 처리하고 확장 프로그램에 확인을 보냅니다. 그러나 확인서를 내선 번호로 보내는 방법을 알 수 없었습니다.

사용자가 링크를 클릭하면 어떻게 확인 메시지를 내선 번호로 다시 보낼 수 있습니까? 다시 XMLHttpRequest을 사용합니까? 방법? 그리고 연장선에두기 위해 어떤 청취자가 필요합니까?

class AuthReceive(InboundMailHandler): 
    def receive(self, message): 

#--------------------------------------- 
     #receive email // I know this part 
     #send confirmation to extension // I need help with this 
#-------------------------------------- 
... 

그리고 background.html에 도움을

//... 
// add listener and save the variable to localStorage 
//... 

감사 : 아래

는 응용 프로그램에서 AuthReceive 핸들러의 스케치입니다.

UPDATE는

Moishe's answer을 준수하는 코드이다. 마지막 단계를 제외한 모든 작업이 수행됩니다. handler.onmessage이 실행되지 않습니다.

options.html

<html> 
<head><title>Extension Options</title></head> 
<body> 
<p>Enter your gmail address:</p> 

<textarea id="getEmail" style="margin-bottom: 4px; width: 250px; height: 20px"> 
</textarea><br /> 

<button id="save">Save</button> 
<!--<button id="save">Clear</button>--> 

<script type="text/javascript"> 

document.getElementById("getEmail").placeholder = "your gmail address" ; 

//send entered gmail address to the server 
document.getElementById("save").addEventListener 
(
    "click", 
    function() 
    { 
     var userEmail = document.getElementById("getEmail").value; 
     var formData = new FormData(); 
     formData.append("extension_user", userEmail); 

     var channel; 
     var socket; 
     var handler = new Object(); 
     handler.onmessage = 
     function (evt) 
     { 
      //evt.data will be what the server sends in channel.send_message 
      console.log("evt.data received from authhandler: " + evt.data); 
     };  

     var xhr = new XMLHttpRequest(); 
     xhr.onReadyStateChange = function() 
     { 
      //error handling etc not included 
      if (xhr.readyState == 4 && xhr.status == 200) 
      { 
       token = xhr.responseText; 
       channel = new goog.appengine.Channel(token); 
       socket = channel.open(handler); 
      } 
     }; 
     xhr.open("POST", "http://ting-1.appspot.com/authsender", true); 
     xhr.send(formData); 
     console.log("formData sent to authsender: " + formData); 

    }, false 
) 
//... 
</script> 
</body> 
</html> 

AuthSender 및 AuthHandler

class AuthSender(webapp.RequestHandler): 
    def post(self): 
     new_user = self.request.get("extension_user") 
     link = "http://ting-1.appspot.com/authhandler?new_user=%s" % new_user 

     message = mail.EmailMessage() 
     message.sender="Ting <[email protected]>" 
     message.to=new_user 
     message.subject="Ting Bookmarking - Confirm your email" 
     message.body="""Click on the link to confirm your email: %s """ % link 
     message.send() 

     logging.info("message sent to: %s " % message.to) 

     token = channel.create_channel(new_user) 
     self.response.out.write(token) 

class AuthHandler(webapp.RequestHandler): 
    def get(self): 
     new_user = self.request.get("new_user") 

     channel.send_message(new_user, new_user) 

     logging.info("new_user sent to client: %s" % new_user) 


작업 버전

(Related question)

options.html

<html> 
<head> 
    <title>Extension Options</title> 
    <!-- this does not work because it is local url 
    <script type="text/javascript" src="/_ah/channel/jsapi"></script> 
    --> 
    <script type="text/javascript" src="https://talkgadget.google.com/talkgadget/channel.js"></script> 
</head> 

<body> 
<p>Enter your gmail address:</p> 

<textarea id="getEmail" style="margin-bottom: 4px; width: 250px; height: 20px"> 
</textarea><br /> 

<button id="save">Save</button> 

<script> 
document.getElementById("getEmail").placeholder = "your gmail address" ; 

document.getElementById("save").addEventListener 
(
    "click", 
    function() 
    { 
     var userEmail = document.getElementById("getEmail").value; 
     var formData = new FormData(); 
     formData.append("extension_user", userEmail); 

     var channel; 
     var socket; 
     var handler = 
     { 
      onopen: function() { alert("onopen") }, 
      onerror: function() { alert("onerror") }, 
      onclose: function() { alert("onclose") }, 
      onmessage: 
      function (evt) 
      { 
       alert("evt.data is: " + evt.data) 
      } 
     };  

     var xhr = new XMLHttpRequest(); 
     xhr.onreadystatechange = function() 
     { 
      if (xhr.readyState == 4 && xhr.status == 200) 
      { 
       token = xhr.responseText; 
       channel = new goog.appengine.Channel(token); 
       socket = channel.open(handler); 
      } 
     }; 
     xhr.open("POST", "http://ting-1.appspot.com/authsender", true); 
     xhr.send(formData);    
    }, false 
) 
</script> 
</body> 
</html> 

가 AuthSender 및 AuthHandler

class AuthSender(webapp.RequestHandler): 
    def post(self): 
     new_user = self.request.get("extension_user") 
     link = "http://ting-1.appspot.com/authhandler?new_user=%s" % new_user 

     message = mail.EmailMessage() 
     message.sender="Ting <[email protected]>" 
     message.to=new_user 
     message.subject="Ting Bookmarking - Confirm your email" 
     message.body="""Click on the link to confirm your email: %s """ % link 
     message.send() 

     token = channel.create_channel(new_user) 

     self.response.out.write(token) 

class AuthHandler(webapp.RequestHandler): 
    def get(self): 
     new_user = self.request.get("new_user") 

     self.response.out.write(new_user) 

     channel.send_message(new_user, new_user) 
+0

"전자 메일"이란 의미는 "전자 메일 * 주소"입니까? – kindall

+0

@kindall; 네, 명확히 해줘서 고마워요. 나는'userEmail = message.sender'와 같이 앱의'AuthReceive' 핸들러에서 사용자의 이메일 주소를 얻고이 정보를 내선 번호로 전송할 것을 제안합니다. 확장 프로그램에'userEmail'을 보내는 방법을 찾으려고합니다. 다시 한번 감사드립니다. – Zeynel

+0

자신의 인증 솔루션을 계속 구성하려고합니다. Chrome-to-Phone의 문제점은 무엇입니까? –

답변

4

이이 작업을 성취하기 위해 사용할 수 Channel API. XMLHttpRequest를 만들 때 토큰을 요청하고 채널을 열어 메시지를 수신합니다.앱에서 링크를 클릭하는 사용자에게 해당하는 HTTP 요청을 처리하면 해당 사용자의 확장에 대해 생성 된 채널로 메시지를 보냅니다.

더 많은 세부 사항 : 당신이 XHR을 할 때

기본적으로 options.html,이 같은 수행 서버에서 다음

var channel; 
var socket; 
var handler = { 
    onmessage: function(evt) { 
    // evt.data will be what your server sends in channel.send_message 
    } 
}; 
var xhr = new XMLHttpRequest(); 
xhr.onReadyStateChange = function() { 
    // error handling and whatnot elided 
    if (xhr.readyState == 4 and xhr.status == 200) { 
    // We got a response from the server. The responseText is 
    // a channel token so we can listen for a "verified" message. 
    token = xhr.responseText; 
    channel = new goog.appengine.Channel(token); 
    socket = channel.open(handler); 
    } 
}; 
xhr.open("POST", "http://ting-1.appspot.com/authsender", true); 
xhr.send(formData); 

을의 'authsender'페이지에 대한 핸들러가 할 것 다음과 같은 내용이 있습니다.

class AuthSenderHandler(webapp.RequestHandler): 
    def post(self): 
    # get whatever data is in the form to send an email. 
    # let's say that user_id is a field we extracted either from a cookie or from 
    # the POST parameters themselves. 
    link = "http://your.server.com/authHandler?user_id=%s" % user_id 
    message = mail.EmailMessage() 
    message.body = """some stuff %s""" % link 
    # fill in other message fields, then send it. 

    # now we'll create a channel token using the user_id and return 
    # it to the client. 
    token = channel.create_channel(user_id) 
    self.response.out.write(token) 

위의 두 함수를 사용하면 클라이언트에서 채널을 수신하게됩니다. 다음 단계는 사용자가 링크를 클릭하면 어떻게됩니까?

이전에는 user_id 매개 변수가 포함 된 전자 메일로 링크를 보냈습니다 (설명을 위해 다른 것을 사용하고 싶을 수도 있음). 이제 사용자가 링크를 클릭하면 user_id 매개 변수로 authHandler 경로로 HTTP 요청을합니다.

class AuthHandler(webapp.RequestHandler): 
    def get(self): 
    user_id = self.request.get("user_id") 
    # send a message indicating the user received the email and 
    # verified to our client page. You may want to send other data. 
    channel.send_message(user_id, "authorized") 
    # probably want to show a nice "you've been authorized" page or something 

을 그리고 다음 handler.onmessage 콜백이 호출됩니다 당신은 당신이 사용자가 확인하는 것이 지금해야 할 무엇이든 할 수 있습니다 그래서 우리는 이렇게 같이에게 메시지를 보낼 수있는 채널을 식별 할 USER_ID를 사용할 수 있습니다 그들의 이메일 주소.

희망이 조금 있습니다!

+0

답장을 보내 주셔서 감사합니다. 나는 질문이있다. 당신은 HMLHttpRequest로 토큰을 요청해야한다고 말합니다. 어떻게 이뤄지나요? 나는 아직도 문서를 연구 중이다. 그리고 나의 이해는 앱 (클라이언트가 아닌)이 채널을 열어야한다는 것이다. 주문에 대한 더 많은 단서를 줄 수 있습니까? 예를 들어, 클라이언트 id = 12345; 나는'channel.create_channel (12345)'가 채널을 열 것이라고 가정한다. 이 올바른지? 다시 한번 감사드립니다. – Zeynel

+0

채널 API에 대한 새로운 질문을 한 번 보았습니다. http://stackoverflow.com/questions/7942814/how-can-i-use-google-app-engine-python-channel-api- with-chrome-extension 감사합니다. – Zeynel

+1

추가 세부 사항 위의 추가; 나는 그것이 다른 질문에 대한 필요성을 없애 준다고 생각한다. –

관련 문제