Blame src/examples/lastfm-shout.c

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