<commit_before>from google.appengine.api import oauth
import webapp2
import json

class UserInfo(webapp2.RequestHandler):
    def get(self):
        userinfo = {}

        try:
            user = oauth.get_current_user()
            userinfo['id'] = user.user_id()
            userinfo['email'] = user.email()
            userinfo['nickname'] = user.nickname()

        except oauth.OAuthRequestError, e:
            return self.error(401)

        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(json.dumps(userinfo))

app = webapp2.WSGIApplication([('/oauth/v1/userinfo', UserInfo)])<commit_msg>Added JSON encoded error output<commit_after>from google.appengine.api import oauth
import webapp2
import json

class UserInfo(webapp2.RequestHandler):
    def get(self):
        userinfo = {}
        errormsg = None

        try:
            user = oauth.get_current_user()
            if user:
                userinfo['id'] = user.user_id()
                userinfo['email'] = user.email()
                userinfo['nickname'] = user.nickname()

        except oauth.InvalidOAuthTokenError, e:
            errormsg = 'invalid token'

        except oauth.OAuthRequestError, e:
            errormsg = 'invalid header'

        except oauth.OAuthServiceFailureError, e:
            errormsg = 'service failure'

        if errormsg:
            error = get_error(errormsg)
            self.error(error['error']['code'])

            data = json.dumps(error)

        else:
            data = json.dumps(userinfo)

        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(data)

def get_error(message, code=401, location='header'):
    return {
      'error': {
        'errors': [
          {
            'domain': 'com.google.auth',
            'reason': 'invalidAuthentication',
            'message': message,
            'locationType': location,
            'location': 'Authorization'
          }
        ],
        'code': code,
        'message': message
      }
    }

app = webapp2.WSGIApplication([('/oauth/v1/userinfo', UserInfo)])
{'fraction_non_alphanumeric': 0.1218250235183443, 'fraction_numerical': 0.00658513640639699, 'mean_word_length': 2.096069868995633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3769311', 'n_tokens_mistral': 627, 'n_tokens_neox': 610, 'n_words': 117}