简体   繁体   中英

Unable to store data in appengine datastore

I'm trying to make a simple appengine app where users login through Github and their public information(only userid) is stored in the datastore. Here is the simple script which I wrote:

import webapp2
from google.appengine.ext import db

import json
import requests

form="""
<html>
<body>
<a href="https://github.com/login/oauth/authorize?state=myapp&redirect_uri=(redirection uri)&client_id=(myclientid)&scope=user">Login</a>
</body>
</html>
"""

class Userdata(db.Model):
    userid = db.StringProperty()

class Devhandler(webapp2.RequestHandler):
    def get(self):
        url_get = self.request.GET
        state=url_get['state']
        code=url_get['code']
        self.response.headers["Content-Type"] = "application/json"
        url = 'https://github.com/login/oauth/access_token?client_id=(clientid)&client_secret=(my secretclient id)&redirect_uri=(redirect uri)&code='+code
        r=requests.post(url)
        req=str(r.content)
        access_token = ""
        i=13
        while(req[i]!='&'):
            access_token = access_token + req[i]
            i = i + 1

        result_url = 'https://api.github.com/user?access_token='+str(access_token)
        result = requests.get(result_url)
        fd = json.loads(result.content) #fd contains the user data in json format!
        userid = fd['login'] 
        user_instance = Userdata(userid=userid)
        user_instance.put()
        self.redirect('/welcome')


class Profilehandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write("Welcome!")



app = webapp2.WSGIApplication([
    ('/',Mainhandler),
    ('/dev',Devhandler),
    ('/welcome',Profilehandler)
    ],debug=True)

When I run the above code,everything works fine but the only problem is, I'm unable to store the user data. When I open the appengine data store viewer, I find nothing(no users in the data store!). What is the problem with the code? Why am I unable to store user data?

PS: There is not problem with my account, I can store data in other projects easily. I feel glad if someone can help me out!!

You might be checking the user data in SDK Console data viewer. Try checking the data store at https://appengine.google.com/dashboard . You would surely find your data there if there is no bug in your code.

Are you sure get method of davhandle class is getting called? Use some logging to test it. just incase if you dont know:

import logging
logging.info('anythng')

  • Your Mainhandler dose not exist in the code.
  • Why you need request ?
  • It's better for your project if you use directly ndb in place of db.
  • Are you shure you don't refresh the page /welcome in place of / ?

If your implement this, the string value should be store in the datastore. If not you have another problem.

class Userdata(db.Model):
  userid = db.StringProperty()

class Mainhandler(webapp2.RequestHandler):
  def get(self):
    user_instance = Userdata(userid="test")
    user_instance.put()
    self.redirect('/welcome')

Add a logging in your code to see the content of userid and force this content with str before storing. user_instance = Userdata(userid=str(userid))

if the conversion in str is not possible it's a character encoding problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM