Blame src/examples/lastfm-shout.c

Packit Service c6b9b0
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
Packit Service c6b9b0
/*
Packit Service c6b9b0
 * Copyright © 2015 Felipe Borges
Packit Service c6b9b0
 *
Packit Service c6b9b0
 * This library is free software; you can redistribute it and/or
Packit Service c6b9b0
 * modify it under the terms of the GNU Lesser General Public
Packit Service c6b9b0
 * License as published by the Free Software Foundation; either
Packit Service c6b9b0
 * version 2 of the License, or (at your option) any later version.
Packit Service c6b9b0
 *
Packit Service c6b9b0
 * This library is distributed in the hope that it will be useful,
Packit Service c6b9b0
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service c6b9b0
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service c6b9b0
 * Lesser General Public License for more details.
Packit Service c6b9b0
 *
Packit Service c6b9b0
 * You should have received a copy of the GNU Lesser General
Packit Service c6b9b0
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit Service c6b9b0
 */
Packit Service c6b9b0
Packit Service c6b9b0
#include <config.h>
Packit Service c6b9b0
Packit Service c6b9b0
#define GOA_API_IS_SUBJECT_TO_CHANGE
Packit Service c6b9b0
#include <goa/goa.h>
Packit Service c6b9b0
Packit Service c6b9b0
#include <locale.h>
Packit Service c6b9b0
#include <rest/rest-proxy.h>
Packit Service c6b9b0
#include <rest/rest-proxy-call.h>
Packit Service c6b9b0
Packit Service c6b9b0
int
Packit Service c6b9b0
main (int argc, char **argv)
Packit Service c6b9b0
{
Packit Service c6b9b0
  GError *error = NULL;
Packit Service c6b9b0
  GoaClient *client;
Packit Service c6b9b0
  GList *accounts, *l;
Packit Service c6b9b0
  GoaAccount *account;
Packit Service c6b9b0
  GoaOAuth2Based *oauth2 = NULL;
Packit Service c6b9b0
  char *access_token = NULL;
Packit Service c6b9b0
  RestProxy *proxy;
Packit Service c6b9b0
  RestProxyCall *call;
Packit Service c6b9b0
  gchar *sig, *api_signature;
Packit Service c6b9b0
  const char *user;
Packit Service c6b9b0
  const char *message = NULL;
Packit Service c6b9b0
Packit Service c6b9b0
  setlocale (LC_ALL, "");
Packit Service c6b9b0
Packit Service c6b9b0
  if (argc != 2 && argc != 3) {
Packit Service c6b9b0
    g_print ("Usage: %s USER [MESSAGE]\n", argv[0]);
Packit Service c6b9b0
    return 1;
Packit Service c6b9b0
  }
Packit Service c6b9b0
  user = argv[1];
Packit Service c6b9b0
  message = argv[2];
Packit Service c6b9b0
Packit Service c6b9b0
  client = goa_client_new_sync (NULL, &error);
Packit Service c6b9b0
  if (!client) {
Packit Service c6b9b0
    g_error ("Could not create GoaClient: %s", error->message);
Packit Service c6b9b0
    return 1;
Packit Service c6b9b0
  }
Packit Service c6b9b0
Packit Service c6b9b0
  accounts = goa_client_get_accounts (client);
Packit Service c6b9b0
  for (l = accounts; l != NULL; l = l->next) {
Packit Service c6b9b0
    account = goa_object_get_account (GOA_OBJECT (l->data));
Packit Service c6b9b0
    if (g_strcmp0(goa_account_get_provider_name (account), "Last.fm") == 0) {
Packit Service c6b9b0
        g_object_ref (account);
Packit Service c6b9b0
        oauth2 = goa_object_get_oauth2_based (GOA_OBJECT (l->data));
Packit Service c6b9b0
        break;
Packit Service c6b9b0
    }
Packit Service c6b9b0
  }
Packit Service c6b9b0
Packit Service c6b9b0
  g_list_free_full (accounts, (GDestroyNotify) g_object_unref);
Packit Service c6b9b0
Packit Service c6b9b0
  g_assert (account);
Packit Service c6b9b0
  g_assert (oauth2);
Packit Service c6b9b0
Packit Service c6b9b0
  if (!goa_oauth2_based_call_get_access_token_sync (oauth2, &access_token, NULL, NULL, &error)) {
Packit Service c6b9b0
    g_error ("Could not get access token %s\n", error->message);
Packit Service c6b9b0
    return 1;
Packit Service c6b9b0
  }
Packit Service c6b9b0
Packit Service c6b9b0
  g_print ("Got access token\n");
Packit Service c6b9b0
Packit Service c6b9b0
  sig = g_strdup_printf ("api_key%s"
Packit Service c6b9b0
                         "message%s"
Packit Service c6b9b0
                         "methoduser.shout"
Packit Service c6b9b0
                         "sk%s"
Packit Service c6b9b0
                         "user%s%s",
Packit Service c6b9b0
                         goa_oauth2_based_get_client_id (oauth2),
Packit Service c6b9b0
                         message,
Packit Service c6b9b0
                         access_token,
Packit Service c6b9b0
                         user,
Packit Service c6b9b0
                         goa_oauth2_based_get_client_secret (oauth2));
Packit Service c6b9b0
Packit Service c6b9b0
  api_signature = g_compute_checksum_for_string (G_CHECKSUM_MD5, sig, -1);
Packit Service c6b9b0
Packit Service c6b9b0
  proxy = rest_proxy_new ("https://ws.audioscrobbler.com/2.0/", FALSE);
Packit Service c6b9b0
  call = rest_proxy_new_call (proxy);
Packit Service c6b9b0
  rest_proxy_call_set_method (call, "POST");
Packit Service c6b9b0
  rest_proxy_call_add_header (call, "Content-Type", "application/x-www-form-urlencoded");
Packit Service c6b9b0
  rest_proxy_call_add_param (call, "api_key", goa_oauth2_based_get_client_id (oauth2));
Packit Service c6b9b0
  rest_proxy_call_add_param (call, "method", "user.shout");
Packit Service c6b9b0
  rest_proxy_call_add_param (call, "message", message);
Packit Service c6b9b0
  rest_proxy_call_add_param (call, "user", user);
Packit Service c6b9b0
  rest_proxy_call_add_param (call, "sk", access_token);
Packit Service c6b9b0
  rest_proxy_call_add_param (call, "api_sig", api_signature);
Packit Service c6b9b0
Packit Service c6b9b0
  if (!rest_proxy_call_sync (call, &error)) {
Packit Service c6b9b0
    g_error ("Cannot shout message to user %s: %s", user, error->message);
Packit Service c6b9b0
    return 1;
Packit Service c6b9b0
  }
Packit Service c6b9b0
Packit Service c6b9b0
  g_print ("Message sent!\n");
Packit Service c6b9b0
Packit Service c6b9b0
  return 0;
Packit Service c6b9b0
}