Changeset 56b57b1a for lib/oauth2.c


Ignore:
Timestamp:
2015-02-22T17:13:51Z (9 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Children:
4f6dfbb
Parents:
ee170a1
Message:

oauth2.c now uses parson. Twitter does not yet and I will fix that later.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/oauth2.c

    ree170a1 r56b57b1a  
    4242#include "oauth2.h"
    4343#include "oauth.h"
    44 #include "json.h"
    45 #include "json_util.h"
     44#include "parson.h"
    4645#include "url.h"
     46
     47#define JSON_O_FOREACH(o, k, v) \
     48    const char *k; const JSON_Value *v; int __i; \
     49    for (__i = 0; json_object_get_tuple(o, __i, &k, &v); __i++)
    4750
    4851char *oauth2_url(const struct oauth2_service *sp)
     
    113116}
    114117
    115 static char* oauth2_parse_error(json_value *e)
     118static char* oauth2_parse_error(const JSON_Value *e)
    116119{
    117120        /* This does a reasonable job with some of the flavours of error
    118121           responses I've seen. Because apparently it's not standardised. */
    119122
    120         if (e->type == json_object) {
     123        if (json_type(e) == JSONObject) {
    121124                /* Facebook style */
    122                 const char *msg = json_o_str(e, "message");
    123                 const char *type = json_o_str(e, "type");
    124                 json_value *code_o = json_o_get(e, "code");
     125                const char *msg = json_object_get_string(json_object(e), "message");
     126                const char *type = json_object_get_string(json_object(e), "type");
     127                JSON_Value *code_o = json_object_get_value(json_object(e), "code");
    125128                int code = 0;
    126129
    127                 if (code_o && code_o->type == json_integer) {
    128                         code = code_o->u.integer;
     130                if (json_type(code_o) == JSONNumber) {
     131                        code = json_value_get_number(code_o);
    129132                }
    130133
    131134                return g_strdup_printf("Error %d: %s", code, msg ? msg : type ? type : "Unknown error");
    132         } else if (e->type == json_string) {
    133                 return g_strdup(e->u.string.ptr);
     135        } else if (json_type(e) == JSONString) {
     136                return g_strdup(json_string(e));
    134137        }
    135138        return NULL;
     
    150153        if (content_type && (strstr(content_type, "application/json") ||
    151154                             strstr(content_type, "text/javascript"))) {
    152                 json_value *js = json_parse(req->reply_body, req->body_size);
    153                 if (js && js->type == json_object) {
    154                         JSON_O_FOREACH(js, k, v){
     155                JSON_Value *js = json_parse_string(req->reply_body);
     156                if (js && json_type(js) == JSONObject) {
     157                        JSON_O_FOREACH(json_object(js), k, v){
    155158                                if (strcmp(k, "error") == 0) {
    156159                                        error = oauth2_parse_error(v);
    157160                                }
    158                                 if (v->type != json_string) {
     161                                if (json_type(v) != JSONString) {
    159162                                        continue;
    160163                                }
    161164                                if (strcmp(k, "access_token") == 0) {
    162                                         atoken = g_strdup(v->u.string.ptr);
     165                                        atoken = g_strdup(json_string(v));
    163166                                }
    164167                                if (strcmp(k, "refresh_token") == 0) {
    165                                         rtoken = g_strdup(v->u.string.ptr);
     168                                        rtoken = g_strdup(json_string(v));
    166169                                }
    167170                        }
Note: See TracChangeset for help on using the changeset viewer.