0
페이스 북 앱을 만들고 자바 스크립트 API를 사용하여 사용자 이름 및 기타 세부 정보를 js 변수에 저장합니다. 그리고이 응용 프로그램은 Python (Google App Engine 사용) 아래에 만들어집니다. 파이썬에서 js 변수에 액세스하여 Db에 값을 저장하려고합니다. 이 작업을 수행하는 방법?파이썬에서 자바 스크립트 변수에 액세스하는 방법
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write("""
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://ogp.me/ns/fb#">
<head>
<title>Captain Pandey</title>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '185318768251247',
channelURL : 'http://localhost:8080/channel.html',
status : true,
cookie : true,
xfbml : true,
oauth : true,
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
FB.api('/me', function(apiResponse) {
var name = apiResponse.name;
});
}
else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
}
else {
// the user isn't logged in to Facebook.
}
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
<div class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1" scope="email">Login to Facebook</div>
</body>
</html>
""")
및 사용 템플릿 webapp2입니다
아시다시피 JS 변수는 클라이언트 측에 존재하며 파이썬 코드는 서버 측에 있습니다. 그들과 통신하는 유일한 방법은 HTTP (예 : AJAX)를 사용하는 것입니다. 직접 액세스 할 수 없습니다. – bereal
자바 스크립트에서 어떤 변수를 저장하고 어떤 시점에서 어떤 제어 로직을 사용하는지 보여주는 코드를 마크 업하는 것이 도움이됩니다. –